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.

 

 

Example

Username:
Password:
 

 

Script Source

 

<The Form >

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

<The php script login.php >

<?php
error_reporting(0);
session_start();

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

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

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

// Script by www.wizzardweb.co.uk
// Please do not remove this information -- Copyright 2007
?>

 

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

[rating]