Page 1 of 1

login script to blank page

Posted: Wed Dec 22, 2004 6:43 pm
by C_Calav
can anyone see anything wrong with this?

submiting to a blank page??

Code: Select all

<?php
	$username = $_POST["username"]; 
	$password = $_POST["password"]; 

	if(!empty($_POST['submit'])) 
	{ 

     mysql_connect('localhost','chris','survey') or die ("Could not connect to database");
	  mysql_select_db('survey') or die ("Could not select database!" .mysql_error()); 
     
     $sql = "select * from tbl_admin where a_username = '$username'"; 
     $result = mysql_query($sql) or die("Execution failed: ".mysql_error());

     while ($row=mysql_fetch_array($result)) 
     { 
	    if ($row["a_password"] == $_POST["password"]) 
       { 
    	 	echo"wrong passwrod";

        	$_SESSION["name"] = $username; 
        	$_SESSION["loggedin"] = "set"; 
   	 } 
       else 
       { 
         echo "wrong password"; 
       } 

	  }

	}
?>

Code: Select all

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;style type="text/css" media="all"&gt;
		@import "/css/survey.css";
	&lt;/style&gt;

&lt;script language="JavaScript" type="text/JavaScript"&gt;

function Login() 
&#123;
var errors = false;

if (window.document.login.username.value == '') 
&#123;
  alert("Username is a required field.");
  window.document.login.username.focus(); return (false); errors = true;
&#125;

if (window.document.login.password.value == '') 
&#123;
  alert("Password is a required field.");
  window.document.login.password.focus(); return (false); errors = true;
&#125;		

if (errors == false) 
&#123; 
  document.login.submit(); 
&#125; 
&#125;

&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;form name="login" action="&lt;?echo $PHP_SELF?&gt;" method="post"&gt;

&lt;div id="main_container"&gt;

&lt;div id="container"&gt;

&lt;img src="/images/login_logo.gif" height="101" width="150" alt="login_logo" class="login_logo" /&gt;

&lt;div id="login"&gt;

&lt;p&gt;
&lt;table&gt;
	&lt;tr&gt;
		&lt;td&gt;Username:&lt;/td&gt;
		&lt;td&gt;&lt;input name="username" id="username" type="text" class="inputtext"/&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;Password:&lt;/td&gt;
		&lt;td&gt;&lt;input name="password" id="password" type="password" class="inputtext" /&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
	&lt;td colspan="2" align="right"&gt;&lt;input name="login" type="button" value="Login" class="inputbutton" onClick="javascript:Login();"/&gt;&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

Posted: Wed Dec 22, 2004 7:28 pm
by C_Calav
solved, wasnt getting past this

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

for sum reason?

Posted: Wed Dec 22, 2004 7:30 pm
by John Cartwright
because you do not have an input field named "submit".........

you should never rely on the button to be pressed anyways, what if someone presses enter instead of hitting the button?

Posted: Wed Dec 22, 2004 8:03 pm
by C_Calav
thanx for the tip Phenom will take that into account for the future!

Posted: Fri Dec 24, 2004 2:19 pm
by npeelman
Phenom wrote:because you do not have an input field named "submit".........

you should never rely on the button to be pressed anyways, what if someone presses enter instead of hitting the button?
I beleive hitting <enter> also submits the 'submit' post variable.

Norm

Posted: Fri Dec 24, 2004 3:43 pm
by Hibba
Yes it does, in most all browsers, but not IE, at least not always. See this posting for a similiar problem:
http://www.sitepoint.com/forums/showthread.php?t=46675

Merry Christmas!

Posted: Fri Dec 24, 2004 4:03 pm
by rehfeld
good to know...[runs quickly to edit all code done in the last year]

Posted: Sat Dec 25, 2004 6:08 am
by npeelman
Hibba wrote:Yes it does, in most all browsers, but not IE, at least not always. See this posting for a similiar problem:
http://www.sitepoint.com/forums/showthread.php?t=46675

Merry Christmas!
Ok, but that is an IE5 bug... has anyone tried it with 6 yet. Not that it really matters as you the programmer knows whether or not you only have one field in your form. And doesn't adding a hidden field make more than one field in the form... allowing the enter key to POST the submit variable?

Norm

Posted: Sat Dec 25, 2004 8:20 am
by Hibba
Well, some think if hitting enter submits the form, then THAT is the bug... And those people would be MS employees who wrote this crappy code... BUT adding a hidden field to the on field form does not help it, it has to be a non-hidden field.
I can't say I have tried it for IE6, so I am not sure. All I know is when the problem arose, I neede to fix it quick.

Posted: Sat Dec 25, 2004 9:15 am
by npeelman
Hibba wrote:Well, some think if hitting enter submits the form, then THAT is the bug... And those people would be MS employees who wrote this crappy code... BUT adding a hidden field to the on field form does not help it, it has to be a non-hidden field.
I can't say I have tried it for IE6, so I am not sure. All I know is when the problem arose, I neede to fix it quick.
As long as you check for the variables you are looking for, I guess you really don't need the submit...

Norm

Posted: Sat Dec 25, 2004 12:28 pm
by Hibba
This is true, but what better way to see if a form had been 'submitted' than the SUBMIT button! It only makes sense.
If(isset($submit) {
blah blah
}

you know?