Create Remember me login using php

I had previously made a tutorial on the use of php session at the following link, and now I want create a tutorial to make “remember me function” on form login page. So to make it, then we need “cookies”. What it is cookies?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

For more info, see on this link or this
Now I will try to make a remember me feature using these cookies. So if we use cookies (example remember me feature) we will stay logged in to the admin page without login again, even though we have closed the browser. With note the cookies not expired.

  1. First we use the code on the previous tutorial. And I modify like this:
    class User
    	//private variabel for session data
    	private $user;
    	//function for check username and password
    	function check_login($username, $password) {
    		//get username value and save to private variabel
    		$this->user = $username;
    		//checked username and password
    		if($username == "admin" && $password == "password") {
    			//if username and password true, then create session.
    			$_SESSION['login'] = true;
    			$_SESSION['id'] = $this->user;
    			return true;
    		} else {
    			//if username and password false
    			return false;
    	//function for set cookies 1 hour
    	function setCookies($user) {
    		setcookie("user", $user, time()+3600);
    	//function for checking cookies
    	function checkCookies() {
    		if(isset($_COOKIE)) {
    			if(isset($_COOKIE['user'])) {
    				$_SESSION['login'] = true;
    				$_SESSION['id'] = $_COOKIE['user'];
    	//function for checking session
    	function checkSession() {
    		//if user has login and session has not been removed
    		if(isset($_SESSION['login'])) {
    			//redirect to file admin.php and don't need login again
    			header("location: admin.php");
    	//function for delete sessions
    	function session_logout() {
    		//delete all sessions
    		//delete cookie
    		setcookie("user", "", time()-3600);
    		//redirect to form login
    		header("location: index.php");

    This is a main code.

  2. And then this is a login form page:
    //if we use session, use this code
    //include class.php file
    require_once "class.php";
    //instance objek, because we use OOP
    $user = new User();
    //checking cookies
    //checking session
    //if file index.php get post data
    if($_POST) {
    	//checked that post using function check_login (from file class.php)
    	$login = $user->check_login($_POST['username'], $_POST['password']);
    	//if checklist remember me
    	if(isset($_POST['remember'])) {
    	//if user has login
    	if($login) {
    		//if true, redirect to admin.php
    		header("location: admin.php");
    	} else {
    		//if false, we must login again.
    		$error = "Sorry username or password is wrong!!";
    <!DOCTYPE html>
    	<title>Form Login</title>
    	<h1>Form Login</h1>
    	<p><?php echo (isset($error)) ? $error : ""; ?></p>
    	<form method="post" name="login">
    		<label>Username</label><br />
    		<input type="text" name="username" required /><br />
    		<label>Password</label><br />
    		<input type="password" name="password" required /><br />
    		<input type="checkbox" name="remember" />Remember me<br />
    		<input type="submit" value="Login" />
    		<button type="reset">Cancel</button>
  3. And the last is admin page.
    //if we use session, use this code
    //include file class.php
    require_once "class.php";
    //instance objek user from class.php
    $logout = new User();
    //if user open this page and haven't login, redirect to login form
    if(!isset($_SESSION['login'])) {
    //if user click logout link
    if($_GET && $_GET['page']) {
    <!DOCTYPE html>
    	<title>Admin Page</title>
    Hello <b><?php echo $_SESSION['id'] ?>!</b><br />
    <a href="?page=logout">Logout</a>

And now open the index.php file in localhost.
Example result:



  1. Imρressive aɑnd Outsttaոding Imageѕ (1 – 6 November).
    The very basic and simple traffic light to stߋp annd admkt cars onto the California freewasys is just a tiny step and a view of the future.
    , гeal name Matɦangi Arulpragasam, annоսnced on her Mʏ – Space blog on Saturday (Febrսaгy 14) that her bsƅy Ƅorn Feb.

  2. I am extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it’s rare to see a great blog like this one nowadays.

  3. I got this web site from my pal who told me on the topic of this website and at the moment this time I am browsing this website and reading very informative content at this time.

  4. You share interesting things here. I think that your website can go viral easily, but you
    must give it initial boost and i know how to do
    it, just type in google – mundillo traffic increase

  5. Wonderful, you can just change your username cookie to match the administrator’s username and you’ve successfully compromised the website. This is not a secure implementation.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s