I'm trying to create a login page and so far i'm stuck. The login form keeps appearing and the session variables do not seem to be working at all. I still can't figure out what is wrong with my code.
Could someone please take a peek at the code below and see what is wrong? I'm running the PHP with PWS sitting on Windows 98.
Thank you very much.
<?php
//establish user connection
mysql_connect("localhost");
//open up database
mysql_create_db("lwbfaqs");
mysql_select_db("lwbfaqs");
//function to authorise user
function AuthoriseUser($userid, $password)
{
$query = "SELECT userid from user where userid='$userid' and password=password('$password')";
$result = mysql_query($query);
if (!mysql_num_rows($result)) return 0;
else
{
$query_data=mysql_fetch_row($result);
return $query_data[0];
}
}
function login_form()
{
global $PHP_SELF;
?>
<html>
<head>
<title>FAQs administration Login</title>
</head>
<body bgcolor="#EEEEEE">
<FORM METHOD="POST" action="<? echo $PHP_SELF ?>">
<div align='center'><center><b>Please login to access the FAQs Administration Page</b>
<p>
<table width ='40%' style='border: 3px double #800000'>
<tr><td height='20'></td></tr>
<tr><td><font face='arial' size='2'><p align = 'right'>Userid: </font></td><td><input type="userid"></td></tr>
<tr><td><font face='arial' size='2'><p align='right'>Password: </font></td><td><input type="password"></td></tr>
<tr><td height='20'></td></tr>
<tr><td colspan='2'><p align='center'><input type="submit" value="Login" name="B1"> <input type="reset" value="Reset" name="B2"></td></tr>
<tr><td height='20'></td></tr>
</table>
</center></div>
</form>
</body>
</html>
<?
}
session_start();
if (!isset($userid))
{
login_form();
exit;
}
else
{
session_register("userid", "password");
$username=AuthoriseUser($userid, $password);
if (!$username)
{
session_unregister("userid");
session_unregister("password");
echo "Authorisation failed.";
exit;
}
else echo "Welcome!";
}
?>