I have used this, with the database behind it;
Log In Page
<H1>Login</H1>
<P ALIGN=JUSTIFY>Please enter your username and password in the boxes below and login.</P>
<form action='validate.php' method='POST'>
<CENTER>
<table CELLPADDING=0 CELLSPACING=0 CLASS=NOBORDER WIDTH=300>
<tr><td>Username</td><td><input type=text value="" name="Username" SIZE=18></td></tr>
<tr><td>Password</td><td><input type=password value="" name="Password" SIZE=18></td></tr>
<tr><td colspan=2 align=center>
<input type='submit' value='Login'></td></tr></table>
</form>
Validate Page
<html><body><?php
/ Connecting, selecting database /
$link = mysql_connect("localhost", "DATABASENAME", "")
or die("Could not connect");
print "Connected successfully";
mysql_select_db("DATABASENAME") or die("Could not select database");
/* Performing SQL query - to allow names to appear on screen later */
$query = "SELECT `Username` , `FirstName` , `Password` FROM `users` WHERE 1 AND `Username` = '$_POST[Username]' AND `Password` = '$_POST[Password]';";
$result = mysql_query($query) or die("Query failed");
/* Printing results in HTML Change index2.php for your page*/
print "<form action='INDEX2.php' method='POST'><select name='names'>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
print "\t\t<option value='$col_value'>$col_value</option>\n";
}
print "\t\n";
}
print " <input type='submit' value='Submit Data'></form>\n";
$num_of_rows = mysql_num_rows ($query);
/ Free resultset /
mysql_free_result($result);
$query = "SELECT Username , Password , FirstName FROM users WHERE 1 AND Username = '$POST[Username]' AND Password = '$POST[Password]';";
$result = mysql_query($query) or die("Query failed");
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
print "\t\t<input type=hidden value='$col_value'>\n";
$user = $line[Username];
$pass = $line[Password];
$name = $line[FirstName];
print "$col_value";
}
print "\t\n";
}
$info = $result->fields;
$num_of_rows = mysql_num_rows ($query_result_handle);
print "$line[Username]";
/ Closing connection /
mysql_close($link);
if ($num_of_rows = 1) {
print "we have 1 row!!!!!";
}
print $name;
print "<BR><BR>$user<BR>$pass";
if ($name == '') {
header("Location: index2.php");
} Elseif ($user == '') {
header("Location: index2.php");
} Elseif ($pass == '') {
header("Location: index2.php");
} Else {
setcookie("testcookie", "Logged In");
setcookie("user", "$user");
setcookie("pass", "$pass");
header("Location: index2.php");
}
echo "$testcookie";
echo "$HTTP_COOKIE_VARS[testcookie]";
echo $pass;
echo $user;
echo $POST[Username];
echo $POST[Password];
?>
<?php
header("Location: index6.php");
?>
</body>
</html>
ANY OTHER PAGES - Redirects other to login page from any page that you want to restrict to members.
<?php
if ($HTTP_COOKIE_VARS[testcookie]=="Logged In") {
} else {
header("Location: login.php");
}
?>
I hope this is of use.
Regards,
Neil.