No. $_SERVER['PHP_SELF'] should exist always, that's why you should use it. $PHP_SELF only exists when the contents of the $_SERVER array are extracted into global scope. Typically, this means that register_globals is on. We've talked about this before.
I just went through the code I posted and realized that I forgot to convert one of the $PHP_SELFs into $_SERVER['PHP_SELF']. It's in the if block where the username/password combo was bad.
I also noticed that your original code (and, due to bad judgement on my part, mine) use the shorthand <? tags to open a PHP block. This is also something that can be disabled in the PHP configuration, so you should ALWAYS use "<?php" to open a PHP block.
It's a bad idea to use <? or the ASP-style <% delimiters.
Here's a version of the script that works for me:
<?php
session_start();
require_once("common_db.inc");
function auth_user($userid, $userpassword)
{
$query = "SELECT emp_id FROM user WHERE userid = '$userid' AND userpassword = password('$userpassword')";
$result = mysql_query($query);
if ((FALSE === $result) || !mysql_num_rows($result))
{
return FALSE;
}
else
{
return mysql_result($result, 0);
}
}
function login_form()
{
?>
<HTML>
<HEAD>
<TITLE>Login</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="<?php echo $_SERVER['PHP_SELF']; ?>">
<DIV ALIGN="CENTER"><CENTER>
<H3>Please log in to access the page you requested.</H3>
<TABLE BORDER="1" WIDTH="200" CELLPADDING="2">
<TR>
<TH WIDTH="18%" ALIGN="RIGHT" NOWRAP>ID</TH>
<TD WIDTH="82%" NOWRAP>
<INPUT TYPE="TEXT" NAME="userid" SIZE="8">
</TD>
</TR>
<TR>
<TH WIDTH="18%" ALIGN="RIGHT" NOWRAP>Password</TH>
<TD WIDTH="82%" NOWRAP>
<INPUT TYPE="PASSWORD" NAME="userpassword" SIZE="8">
</TD>
</TR>
<TR>
<TD WIDTH="100%" COLSPAN="2" ALIGN="CENTER" NOWRAP>
<INPUT TYPE="SUBMIT" VALUE="LOGIN" NAME="Submit">
</TD>
</TR>
</TABLE>
</CENTER></DIV>
</FORM>
</BODY>
</HTML>
<?php
}
if(!isset($_POST['userid']))
{
login_form();
exit;
}
else
{
$_SESSION['userid'] = $_POST['userid'];
$_SESSION['userpassword'] = $_POST['userpassword'];
$username = auth_user($_SESSION['userid'], $_SESSION['userpassword']);
if(FALSE === $username)
{
unset($_SESSION['userid']);
unset($_SESSION['userpassword']);
echo "Authorization failed. " .
"You must enter a valid userid and password combo. " .
"Click on the following link to try again.<BR>\n";
echo "<A HREF=\"{$_SERVER['PHP_SELF']}\">Login</A><BR>";
echo "If you're not a member yet, click " .
"on the following link to register.<BR>\n";
echo "<A HREF=\"$register_script\">Membership</A>";
exit;
}
else
{
echo "Welcome, $username!";
}
}
?>
Take care,
Nik
http://www.bigaction.org/