login script to blank page

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

login script to blank page

Post 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;
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

solved, wasnt getting past this

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

for sum reason?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx for the tip Phenom will take that into account for the future!
npeelman
Forum Commoner
Posts: 32
Joined: Tue Jul 27, 2004 5:13 am
Location: Oviedo,FL.

Post 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
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post 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!
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

good to know...[runs quickly to edit all code done in the last year]
npeelman
Forum Commoner
Posts: 32
Joined: Tue Jul 27, 2004 5:13 am
Location: Oviedo,FL.

Post 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
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post 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.
npeelman
Forum Commoner
Posts: 32
Joined: Tue Jul 27, 2004 5:13 am
Location: Oviedo,FL.

Post 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
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post 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?
Post Reply