Most
websites have “remember me” or “stay signed in” feature in their login
page. By checking that option you will be able to go to your profile
page or the page after login, without logging in again. If you visit the
home page, you will be redirected to your profile or relevant pages
automatically.
Why “Remember Me” Functionality?
- If a user has a device which is used only by themselves, They can utilize this feature to reduce the time taken for logging in.
- Some
people always forget their login credentials, In this case you don’t
have to worry when you are able to login automatically.
We are going to create a simple login functionality with “Remember Me”
checkbox option. For this tutorial we have four PHP pages such as
index.php,
login.php,
home.php and
logout.php
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?php
session_start();
//if either cookie or session is set, redirect to home page
if((isset($_COOKIE['user']) && $_COOKIE['user'] != '') || (isset($_SESSION['user']) && $_SESSION['user'] !='')){
header("Location: http://domain.com/home.php");
}else{
?>
<!DOCTYPE html>
<html>
<head>
<title>Remember Me</title>
</head>
<body>
<div id="container">
<form action="login.php" method="POST">
<input type="text" name="username" placeholder="username">
<input type="password" name="password" placeholder="password">
<button>Login</button><br><br>
<input type="checkbox" name="remember" value="true"> Remember Me
</form>
</div>
</body>
</html>
<?php
}
?>
|
Index page has a simple
login form with a checkbox named “
remember“. This form data will be sent to
login.php.
At the beginning of the page we are checking whether any
cookie variable or
session variable is set or not. If anyone of it is available we have to redirect users directly to
home.php page.
login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$remember = $_POST['remember'];
$con = mysqli_connect('hostname','username','password','dbname') or Die();
$query = "SELECT id FROM members WHERE username='$username' AND password='$password'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$id = $result['id'];
$cookie_name = "user";
$cookie_value = $id;
//expiriry time. 86400 = 1 day (86400*30 = 1 month)
$expiry = time() + (86400 * 30);
if($remember == 'true'){
//setting cookie variable
setcookie($cookie_name, $cookie_value, $expiry);
}else{
//if your server requires to set session path
session_start();
$_SESSION['user'] = $id;
}
//redirecting to home page
header("Location: http://domain.com/home.php");
?>
|
First
we have to get the id or an equivalent field from database if the login
credentials is matched. If no one is matched you can show some alert
kind of thing.
Then if the checkbox is checked we have to create a
cookie variable named “
user” with the value of the fetched id. We can set
expiry
date or time for the cookie, but it is optional. If you don’t give any
expiry time, the user will be logged in forever until they manually
clear cookies or logout of their account.
If the checkbox is not checked we have to create a
session variable with the name “
user” and the value of the
id.
Finally, we are redirecting the page to
home.page or any profile kind of page.
home.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
session_start();
if(isset($_COOKIE['user']) && $_COOKIE['user'] != ''){
$user = $_COOKIE['user'];
//get user data from mysql
}else if(isset($_SESSION['user']) && $_SESSION['user'] !=''){
$user = $_SESSION['user'];
//get user data from mysql
}else{
header("Location: http://domain.com/");
}
/*
Show user details based on the fetched data
*/
echo "<a href='logout.php'>Logout</a>";
?>
|
At
first, we are checking either a cookie or a session is set or not, if
yes we have to get the user id and fetch user data from database
otherwise we have to
redirect user to
index page.
Also
we have a logout page anchor, by visiting the page user can flush the
session and cookies from the browser to make them no longer logged in.
logout.php
|
<?php
session_start();
//deleting cookie by setting expirty to past time
$res = setcookie('user', '', time() - 3600);
//destroys all session variables
session_destroy();
header("Location: http://domain.com/");
?>
|
There
is no direct method to delete a cookie in PHP, so we are setting a past
time as expiry period, so that the browser will automatically remove
it.
Once everything is done, we have to redirect the page to
index.php
Download and use this code and give your feedback below. Also subscribe for our feed to get notified about posts instantly.
0 comments :