Page 1 of 1

[SOLVED] script help

Posted: Mon Sep 13, 2004 10:44 pm
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>

Posted: Mon Sep 13, 2004 10:54 pm
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 ;)

Posted: Mon Sep 13, 2004 11:19 pm
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.

Posted: Tue Sep 14, 2004 1:25 am
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?

Posted: Tue Sep 14, 2004 2:07 am
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?

extra

Posted: Tue Sep 14, 2004 3:03 am
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.

Posted: Tue Sep 14, 2004 3:15 am
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.

Posted: Tue Sep 14, 2004 4:21 am
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

Posted: Tue Sep 14, 2004 4:39 am
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!

Posted: Tue Sep 14, 2004 4:40 am
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.

Posted: Tue Sep 14, 2004 4:57 am
by C_Calav
thanxs very much CoderGoblin, good spotting! next time will look 8O more carefully :lol:

Posted: Tue Sep 14, 2004 9:59 am
by m3mn0n
Yes. Please do.