Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

errors but works

Status
Not open for further replies.

jen0dorf

IS-IT--Management
Apr 15, 2008
104
GB
Hi

can some nice person tell me where I've gone wrong with this code. It puzzles me that it works but in my test environment throws up these two errors

Notice: Undefined index: username in G:\sites\edwards\login.php on line 7

Notice: Undefined index: password in G:\sites\edwards\login.php on line 8

The code is as below

thanks

Ian

<?php




session_start();
$user = $_REQUEST["username"];
$pass = $_REQUEST["password"];
if ($user == 'admin' && $pass =='starwood') {
$_SESSION['loggedin'] = TRUE;
header( 'Location: test.php' ) ;
} elseif ($user == 'millstream' && $pass =='nicola') {
$_SESSION['loggedin'] = TRUE;
header( 'Location: test.php' ) ;
} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"<html xmlns="<head>
<SCRIPT LANGUAGE="JavaScript">
function placeFocus() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements.type == "text") || (field.elements.type ==
"textarea") || (field.elements.type.toString().charAt(0) == "s")) {
document.forms[0].elements.focus();
break;
}
}
}
}
</script>
<link href="emlogon.css" rel="stylesheet" type="text/css" />
</head>
<body OnLoad="placeFocus()">

<div id="header">
<div id="logo">
<div align="center"><img src="graphics/administration.jpg" alt="admin"
/></div>
</div>
</div>
<center>
<form method="post" action="login.php">
<p>User Name </p>
<p>
<input name="username" type="text">
</p>
<p>Password </p>
<p>
<input name="password" type="password">
</p>
<p>
<input name="Login" type="submit" value="Login">
</p>
</form>
</center>

</body>
</html>
<?php
}

?>
 
It's these two lines:
Code:
$user = $_REQUEST["username"];
$pass = $_REQUEST["password"];
You're trying to read values from form (from cookies, get or post request -- I suggest you use one that is appropriate for your example, i.e. POST, instead of REQUEST, which carries all three) even when the form has not been posted. You would need to move these variable assigns inside an if clause that would first check if the form was even submitted or not.

The code, as you noticed will work, however the script will inform you that you're trying to assign a variable value with a non-existing value.

___________________________________________________________
[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Hi

thanks for that, off to have a read up on post

Ian
 
Is this page invoked by a form submission or did you type the page name in the browser ?
I suspect what is happening is you are calling the page directly so the php executes "fresh" so won't have the username and password available. If the page was processed following a submit from the form they would be.
I would suggest you put a hidden field in you form called postback and set it to 0.
Then detect in you code if the variable postback is in the $_POST array. If you can't see it the page has been caled "fresh" and the HTML to create the postback has not executed yet. When the form is submitted the variable is avaiable and you can then go on to execute the php in confidence.
I expect other will suggest methods such as test username and password and if they are avaiable you are in a post back.
Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top