SD_Ryoko
Ultra Cool Member
- Reaction score
- 85
First, save this into a file with a name. This file does all the work validating a user.
protectit.php
Now on any page that you would like to keep people from login in, all you need are these bits at the top of the file.
protectit.php
PHP:
<?
// Set up your accounts.
$user_passwords = array (
"user1" => "password1",
"user2" => "password2"
);
$valid_user = 0;
//Check login data first, then cookie data.
if(isset($HTTP_POST_VARS['username']) && isset($HTTP_POST_VARS['password']))
{
$username = $HTTP_POST_VARS['username'];
$password = $HTTP_POST_VARS['password'];
}
else
{
$username = $HTTP_COOKIE_VARS['username'];
$password = $HTTP_COOKIE_VARS['password'];
}
// Check if user and pass are set.
if(!isset($username) || !isset($password)) {
$valid_user = 0;
$message="Please login to access this page.";
}
else {
// Login data is correct. Reset (or set) cookie.
if (strcmp($user_passwords[$username],$password) == 0)
{
$valid_user = 1;
setcookie("username", $username, time()+3600);
setcookie("password", $password, time()+3600);
}
else {
// Login data inncorect. Stop user, delete cookie.
$valid_user = 0;
$message="Incorrect login. Please check username and password.";
setcookie("username", $username, time()-3600);
setcookie("password", $password, time()-3600);
}
}
// Stop invalid user. Show form.
if(!($valid_user))
{
?>
<form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST">
<b><? echo $message; ?></b><br/>
Fields are case sensitive.<br/><br/>
<table border="0" cellpadding="5">
<tr><td>Username:</td><td><input type="text" name="username"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"></td></tr>
<tr><td><input type="submit" value="Login"></td><td></td></tr>
</table>
</form>
<?
exit();
}
?>
Now on any page that you would like to keep people from login in, all you need are these bits at the top of the file.
PHP:
<?
include("protectit.php");
?>