How can I add 'remember me' functionality to simpleauth?This is tested working on simpleauth-1.2. AFter completing the following steps, when a user checks the 'remember me' checkbox in the login form, providing they have cookies enabled they will remain logged in across browser sessions.
The method stores the user's login name and md5 hashed password into a cookie called 'simpleauth'. When the checkLoggedIn() function is called, we check to see if a simpleauth cookie exists - if it does then we check to see if the md5 hashed password of the cookie user matches the password hash stored in the cookie - if so then create a clean member session with the data - otherwise set $_SESSION["loggedIn"] to false.
Modifications required follow below - in general copy/paste anything in between -snip- marks:
Add a 'remember me' checkbox to the login form in the login.php script:
<tr><td>Password:</td><td><input type="password" name="password" value="" maxlength="15"></td></tr>
-snip-
<tr><td>Remember Me:</td><td><input type="checkbox" name="remember" <?php isset($_POST["remember"]) && $_POST["remember"] === "on" && print "checked"?>> (requires cookies)</td></tr>
-snip-
<tr><td> </td><td><input name="submit" type="submit" value="Submit"></td></tr>
Add the following to the cleanMemberSession() function in functions.php:
$_SESSION["login"]=$login;
$_SESSION["password"]=$password;
$_SESSION["loggedIn"]=true;
-snip-
// Check if 'remember me' checkbox is set:
if(isset($_POST["remember"]) && $_POST["remember"] === "on"){
// Set a cookie containing user data:
$tmp=array(
"login"=>$login,
"passwd"=>md5($password)
);
setcookie("simpleauth", serialize($tmp), time()+60*60*24*365);
}
-snip-
Add the following to the flushMemberSession() function in functions.php:
// and use session_destroy to destroy all data associated
// with current session:
session_destroy();
-snip-
// Check if a cookie is set:
if(isset($_COOKIE["simpleauth"])){
// Remove the cookie:
setcookie("simpleauth", "", time()-999);
unset($_COOKIE["simpleauth"]);
}
-snip-
Add the following at the top of the 'checkLoggedIn()' function:
function checkLoggedIn($status){
-snip-
global $link;
/*
Function to check whether a user is logged in or not:
This is a function that checks if a user is already logged
in or not, depending on the value of $status which is passed
in as an argument.
If $status is 'yes', we check if the user is already logged in;
If $status is 'no', we check if the user is NOT already logged in.
*/
// First check if cookie exists:
if(isset($_COOKIE["simpleauth"])){
// Unserialize data:
$tmp=unserialize(stripslashes($_COOKIE["simpleauth"]));
// Check if stored login details match those in cookie:
$query="SELECT password FROM users WHERE login='".$tmp["login"]."'";
$result=mysql_query($query, $link)
or die("checkLoggedIn fatal error: ".mysql_error());
// Check exactly one row is found:
if(mysql_num_rows($result)!==1) {
// if more than one entry something messed up:
$_SESSION["loggedIn"]=false;
} else {
// fetch the row containing the user's password:
$row=mysql_fetch_array($result);
// Check passwords match:
if($tmp["passwd"]===md5($row["password"])){
// yes they match, carry on and make clean
// member session:
cleanMemberSession($tmp["login"], $row["password"]);
}
}
}
-snip-
switch($status){
If you're familiar with Unix and the patch utility, there's a patch here that you can apply whilst in the original simpleauth-1.2 directory that makes the changes necessary (make sure no changes have been made to functions.php or login.php or it wont work). To apply the patch from the Unix command-line:
cd /path/containing/simpleauth-1.2/
patch < /path/containing/simpleauth-cookies.patch
The patch file is attached to this post.