[SOLVED] script help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

[SOLVED] script help

Post by C_Calav »

script help

hi again, just changing admin area of site over to css, not sure if to post in here or client side. i think here more appoprate?

here is my script before and after and the new one does not do anything! can anyone see why?????

thanx


before:

Code: Select all

<?php session_start(); ?>

<html>
<head>
<title>Admin - Login</title>
</head>

<body>
<form method=post action="<?echo $PHP_SELF?>">


  <table width="157" border=0 cellpadding=2 cellspacing=0>
    <!--DWLayoutTable-->
    <tr> 
      <td width="75" height="49" align="center" valign="middle"> <div align="left"><img src="../Website_Pics/cessna.jpg" width="69" height="32"></div></td>
      <td colspan="2" valign="middle"><font size="4" face="Linenstroke">Login</font></td>
    
    <tr> 
      <td height="26" colspan="2" valign="top"><font face="Verdana, Arial, Helvetica, sans-serif">Username:</font></td>
      <td width="60" valign="top"><input type="text" name="username" size=10></td>
    <tr> 
      <td height="26" colspan="2" valign="top"><font face="Verdana, Arial, Helvetica, sans-serif">Password:</font></td>
      <td valign="top"><input type="password" name="password" size=10></td>
    <tr> 
      <td height="28" colspan="3" align="center" valign="top"> <input type="submit" name="submit" value="Log In"></td>
    <tr>
      <td height="5"></td>
      <td width="7"></td>
      <td></td>
    </tr>
  </table>
  </form>

  
<?php

$username = $_POST["username"]; 
$password = $_POST["password"]; 

if(!empty($_POST['submit'])) 
{ 
     $db = mysql_pconnect('', '', '') or die ("Could not connect to database"); 
     mysql_select_db('models') or die ("Could not select database!"); 
     $sql = "select * from user where name = '$username'"; 
     $result = mysql_query($sql,$db) or die("Execution failed: ".mysql_error());

     while ($row=mysql_fetch_array($result)) 
     { 
        if ($row["password"]== $_POST["password"]) 
        { 
        echo " ('Successfully Logged In!<a href='admin.php'>Click Here</a>') "; 
        $_SESSION["name"] = $username; 
        $_SESSION["loggedin"] = "set"; 

        } 
      else 
      { 
      echo "wrong password"; 
      } 
  
     } 
} 
?>
</body>
</html>


after:

Code: Select all

<?php session_start(); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Model Aircraft</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">

#Login {
margin-left: 5em;
font: 11px/18px Verdana, Arial, Helvetica, sans-serif;
}

input {
	width: 11em;
	height: 12px;
}

input.button {
	width: 5em;
	height: 22px;
	background-color: #e6e6e6;
	border-color: #ccc #666 #666 #ccc;
	cursor: pointer;
}

</style>


</head>

<body>

<form method=post action="<?echo $PHP_SELF?>">

<!-- Begin Login Div -->
<div id="Login">

Usersname  <br />
<input type="text" name="username">  <br />

Password  <br />
<input type="password" name="password"> <br /> <br />

<input type="submit" name="mailSend" value="Send" class="button" />


<!-- End Login Div -->
</div>

</form>

<?php

$username = $_POST["username"]; 
$password = $_POST["password"]; 

if(!empty($_POST['submit'])) 
{ 
     $db = mysql_pconnect('', '', '') or die ("Could not connect to database"); 
     mysql_select_db('models') or die ("Could not select database!"); 
     $sql = "select * from user where name = '$username'"; 
     $result = mysql_query($sql,$db) or die("Execution failed: ".mysql_error());

     while ($row=mysql_fetch_array($result)) 
     { 
        if ($row["password"]== $_POST["password"]) 
        { 
        echo " ('Successfully Logged In!<a href='admin.php'>Click Here</a>') "; 
        $_SESSION["name"] = $username; 
        $_SESSION["loggedin"] = "set"; 

        } 
      else 
      { 
      echo "wrong password"; 
      } 
  
     } 
} 
?>


</body>

</html>
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

hey what isnt it doing exactly.. the CSS looks a bit funny but not exactl sure why.. your php script looks ok to me.. but I am not sure what is not working.. thanks ;)
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey thanx for the reply,

its not showing the "Successfully Logged In!<a href='admin.php'>Click Here</a>" line after the user logs in.

ha ha the css looks funny, maybe, im new to, but it seems to be displaying fine.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

hmm.. well if it isnt showing anything, then it means it isn't parsing that line, perhaps it isn't being submitted correctly.. check your form action and also the if statement.. did it work before the CSS addition?
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hi thanx for your reply.

yes it worked fine before i changed it to css. could it be the "Successfully Logged In!<a href='admin.php'>Click Here</a>" is not showing up because it is not in the main div or somehting?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

extra

Post by phpScott »

Putting the echo outside of the div tags shouldn't be a problem, the code looks fine. Try just printing out anything if successful and not worry about the href yet.
What happens if you use a incorrect password? anything?

Although you could tighten your code a bit by not declaring

Code: Select all

<?php
$password=$_POST['password'];

?>
Then in your test you use

Code: Select all

<?php
if($row['password']==$_POST['password'])
?>
you can get rid of the first bit if you want.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey thanx,

if you type the wrong password it still does not work.

i took out the a href and still does not work

i copied code and pasted back into 1st html version and DOES work!

must be the CSS some how?

i havent cleaned up php like u said but will try now.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

hey try putting the php before the html.. plus like I said the CSS looks wrong but I havent used CSS in forever so I can't really help you there.. gdlk
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey thanx, tried putting the php before the html and didnt solve it!
the css may look or be wrong or right not sure. but i still think it has to do with the echo and the css. just saying!
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Submit button name has changed between the two codes.... New is mailSend not submit. which you check for in your code:

Code: Select all

if(!empty($_POST['submit']))
should be

Code: Select all

if(!empty($_POST['mailSend']))
or change the name of the submit button (which would appear to make more sense).
Also shouldn't

Code: Select all

while ($row=mysql_fetch_array($result))
be

Code: Select all

while ($row=mysql_fetch_assoc($result))
(not 100% sure as don't use mysql but array result wouldn't return $row['password'] in postgres).



In general when you get problems like this concentrate first on if you are getting the correct HTML tags, don't panic about CSS has it only really deals with formatting. (Perform a view source on your resulting browser page). If the tags are not there CSS does nothing with it and you need to check the code, where necessary putting in extra debug echo statements to see if the program flow is as expected.

As a personal preference I prefer

Code: Select all

if (!isset($_POST['mailSend']))
rather than

Code: Select all

if (!empty($_POST['mailSend']))
. Not sure if (!empty logs a warning if $_POST does not exist.
Last edited by CoderGoblin on Tue Sep 14, 2004 4:58 am, edited 1 time in total.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanxs very much CoderGoblin, good spotting! next time will look 8O more carefully :lol:
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Yes. Please do.
Post Reply