PHP Login Script Using Sessions


The following script uses a simple array of users who are permitted to use the site. The submitted password is compared to the array element whose key is the submitted username. If the two passwords match, the user is logged in, and the auth_user session variable is initialised.

If after login you would like to redirect the user, please contact me and I will provide you with the function. If you would also like an encryption for the following script, again please contact me.






Script Source


<The Form >

<form action="login.php" method="post">
<table border="0">
<td><input type="text" size="10" name="username" /></td>
<td><input type="password" size="10" name="password" /></td>
<input type="submit" value="Try It!" />

<The php script login.php >


$passwords = array("paul" => "123456",
"tom" => "123456",
"shelley" => "mypassword",
"venessa" => "opensesame");

if (!$_POST["username"] or !$_POST["password"]) {
echo "You must enter your username and password";

if ($_POST["password"] == $passwords[$_POST["username"]]) {
echo "Login successful";
$_SESSION["auth_user"] = $_POST["username"];
else {
echo "Login incorrect";

// Script by
// Please do not remove this information -- Copyright 2007


Please let me know if this script was helpful to you.