how to activate remember me checkbox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mang
Forum Newbie
Posts: 8
Joined: Thu Nov 02, 2006 12:43 am

how to activate remember me checkbox

Post by mang »

In the login section there are three fields like
USERNAME :
PASSWORD
:

checkbox : Remember me :- How do i work on this checkbox ? I want to do the functionality Remember me on this computer

How it can be done using php code can u suggest any code plz..Once user check the checkbox at the time of login he should be provided with his user name afterwords when he again comes to log in to his control panel

Plz help me!!!!

Thanking you in advance!
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Using cookies ..

Something like should get u started:

Code: Select all

if (isset($_POST['rememberme'])) { 
setcookie("username", $username, time()+60*60*24*100, "/"); 
setcookie("password", $password, time()+60*60*24*100, "/");
}
then check if a cookies is set:

Code: Select all

if (isset($_COOKIE['username'])) {
$username = $_COOKIE['username'];
}

if (isset($_COOKIE['password'])) {
$password = $_COOKIE['password'];
}

hth
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Another implemenation for same purpose but without encrypting cookies data .

Code: Select all

<?php
if (@$_POST["submit"] <> "") {
	$bValidPwd = false ;

	// Setup variables
	$sUsername = @$_POST["username"];
	$sPassword = @$_POST["password"];
	if (ValidateUser($sUsername, $sPassword)) {

		// Write cookies
		$sLoginType = strtolower($_POST["rememberme"]);
		$expirytime = time() + 365*24*60*60; // change cookie expiry time here
		if ($sLoginType == "a") {
			setCookie(CookieAutoLogin, "autologin", $expirytime);
			setCookie(CookieUserName, $sUsername, $expirytime);
			setCookie(CookiePassword, $sPassword, $expirytime);
		} elseif ($sLoginType == "u") {
			setCookie(CookieAutoLogin, "rememberusername", $expirytime);
			setCookie(CookieUserName, $sUsername, $expirytime);
		} else {
			setCookie(CookieAutoLogin, "", $expirytime);
		}
		$_SESSION[SessionStatus] = "login";
		ob_end_clean();
		header("Location: index.php");
		exit();
	} else {
		$_SESSION[SessionMessage] = "Incorrect user ID or password";
	}
} else {
	if (IsLoggedIn()) {
		if ($_SESSION[SessionMessage] == "") {
			ob_end_clean();
			header("Location: index.php");
			exit();
		}
	} else { // Check auto login
		if (@$_COOKIE[CookieAutoLogin] == "autologin") {
			$sUsername = @$_COOKIE[CookieUserName] ;
			$sPassword = @$_COOKIE[CookiePassword] ;
		}
		if (ValidateUser($sUsername, $sPassword)) {
			ob_end_clean();
			header("Location: index.php");
			exit();
		}
	}
}

?>
Cheers,
Dibyendra
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Multiple cookies feels like overkill. You could generate a completely unrelated or likely very unique hash that uses only a single cookie. This hash could be built from a composite or username, password, the time and some other details and stored in the user record. Myself, I would also update this value every time the user logs in (automatic or not) to help remove the possibility that they are hijacked. This does affect the user experience however as it locks the auto-login ability to a single computer (and browser.)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

how about password encryption and decryption of password using cookie ? a code sample please ...
parka
Forum Commoner
Posts: 52
Joined: Mon Feb 26, 2007 6:48 am

Post by parka »

bob_the _builder wrote:Using cookies ..

Something like should get u started:

Code: Select all

if (isset($_POST['rememberme'])) { 
setcookie("username", $username, time()+60*60*24*100, "/"); 
setcookie("password", $password, time()+60*60*24*100, "/");
}
then check if a cookies is set:

Code: Select all

if (isset($_COOKIE['username'])) {
$username = $_COOKIE['username'];
}

if (isset($_COOKIE['password'])) {
$password = $_COOKIE['password'];
}

hth
I suppose the username and password here will be used for a database connection to verify the legitimate user?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

This thread is over a year old, let it die.
Post Reply