How to add 'Remember Me' in login form using PHP session and cookie

The "Remember Me" checkbox in a login form is used to make user login easy as the user does not have to enter userid every time he/she logs in the system. If this checkbox is checked, a login form will automatically remember the userid next time the user logs in.

how to code Remember me in PHP

In this topic, we will develop a PHP login form with "Remember Me" option using PHP and MySQL. If the user selects the checkbox during login, it will remember the email id and will be displayed next time user opens the login form. We will use PHP session and cookies to implement it. Use PHP setcookie() to create a cookie and read the cookie later.

Two cookies will be saved here, one for email id and other for the checkbox.

Next, in the Login form, we will use below code for input email id and "Remember Me" checkbox.

With this, let us develop a login form using email id and password with "Remember Me" option. If the user selects the checkbox, he or she should see the email id automatically displayed, and the checkbox is checked in the next login.

You can also read below topics where step by step description of the topics are available:

  1. How to Develop a Login form with a Simple Menu in PHP and MySQL
  2. How to display user name on header after login using PHP session
  3. How to develop a login and logout system in PHP and MySQL for beginners
  4. How to code Login, Logout and Change Password in PHP and MySQL

Watch YouTube Video

login and logout in php with mysqlFolders and Files

Below is a screenshot of folder structure and files I am using:

Implement Remember Me in PHP Securely

I have created a folder called 'remember_me' under 'htdocs' as I am using XAMPP. If you are using WAMP, create 'login' folder under 'www'.

Folder 'cfg' - In this folder I have kept dbconnect.php which is used to connect to MySQL database.
Folder 'css' - Custom stylesheet is in this folder

index.php is the home page and header.php is the html header part which is included in home and login pages. The home page is very simple, user can click on Login link and login into the system. Below are the screenshots of home and login pages:

PHP login with remember me function

PHP Sessions Login with remember me

Step 1 - Create a MySQL table to store user details

Let us create a table named 'users' in MySQL database. This table will have user details. I have a database called 'demo'. So 'users' Table will be created in this database. If you have an existing database other than demo you can also use it. Just make sure same table name does not exist already. Table structure is given below:

Remember Me for Login using PHP Session and cookie

Table has 5 columns.

  1. id - it is the primary key and auto incremented
  2. email - email id of the user
  3. name - name of the user
  4. password - password for the user
  5. signup_date - date of signup, default is current date, this is not required for login, used as information purpose only
Create table script for this table is given below, you can use this code to create the table. You can also download from the download section later in this topic.

users.sql

After you create the table and inserted data in it, check that it has below data in it with signup date as current date.

Remember me feature on login page using PHP

Step 2 - Connect to MySQL database (dbconnect.php)

Use below script to connect to MySQL database. Note that we have this MySQL connection php program in 'cfg' folder. This is written once and used in every program where database connection is needed. This will be easy for maintenance and also will enable reusability of the code.

dbconnect.php

mysqli_connect() function is used here to connect to the database.

  1. server - in our case it is localhost
  2. userid - we are using root user
  3. password - no password for user root
  4. database name - demo in our case.

We will include this dbconnect.php in other php programs so that we do not need to write it again in the program. For detail database connection understanding please read topic How to Connect to MySQL database in PHP using mysqli_connect.

Step 3 - Create a Home Page (index.php)

The Home page is very simple. It has a link for the user to login. You can develop your own home page and add login link in it. header.php is used as below:

header.php

PHP session_start() function is used at the beginning. Below code is for index.php.

index.php

You can see I am displaying user name and email id when user is logged in. Using PHP session variable, we can check if user is logged in or not. These PHP Session variables are set after successful login which you will see in next step. If the user is not logged in then Login link is displayed.

Step 4 - Develop a Login form (login.php)

The Login form has an email id and password for the user to login to the system. Along with them, "Remember Me" checkbox is added. PHP cookies will be created if "Remember Me" checkbox is checked by the user before login.

Remember me option using PHP Cookie

Let us look at the code for login.php

login.php

For email id, check if cookie for "email" exists, if exists then assign cookie value in the email field. Similarly, for the checkbox, check if cookie for "remember" exists, if exists, then checkbox is checked.

After the form is submitted and if login is successful, then if "Remember Me" checkbox is checked by the user, set two cookies - one for email id and one for "remember me" checkbox itself. PHP setcookie() function is used to store email id value in a cookie named "remember_email" and this cookie will expire after 365 days. Similarly, "remember" cookie is set. If user unchecks the checkbox or if the checkbox is not checked, we need to remove the cookies. In line 23 and 24, we are removing these two cookies by setting it with a past expiration date.

That's it for our main program login.php. Now I will add code for logout.php as below:

logout.php

Here, all session variables are removed and the session is deleted.

Step 5 - Add CSS (style.css)

Let us add below styles. I have already added style.css in header.php.

style.css

Our development is complete, let us test the application.

remember me cookie phpTest the Application

Make sure in your XAMPP control panel services are running. Open the browser and run localhost/remember_me. You will see the home page displayed. See the below screen, you can see Login link is given:

PHP Login Script with Remember me

Now click on Login link, you will see Login Page is opened.

Remember me functionality in login page using PHP

Now you need to test if "Remember Me" is working correctly. Follow below steps to test it:

  1. In the Login form, enter email id as 'test@test.com', password as '123', check 'Remember Me' checkbox and click on Login button.

    Develop PHP Login form with Remember Me

  2. Click on Logout, you should now see Login link in the home page, but Logout and user name are disappeared.
  3. Now click on login link again. You should see in the form test@test.com is already displayed in the email id field. Also, "Remember Me" checkbox is automatically checked. This is because you check "Remember Me" in previous login and cookies were set.

    PHP Remember me using Cookie

  4. Now, again login with same email id and password, but this time uncheck the checkbox. You will be able to login successfully. Logout and click on Login link again. Now you will not see test@test.com in the email id field, it will be blank and also checkbox will be unchecked.

Hope You could understand all the steps and could test it successfully. You can now play around with it and test more with your own test cases.

How to login with remember me function in phpImportant Note

  1. In this example I am using email id to login. You can use separate userid also for login. In that case, the table should have a column for userid.
  2. I have used login email id (or userid) for "Remember Me". You can also add password to remember, but it may not be a good idea because of security reason.

Watch YouTube Video

download source code Remember me in PHPDownload Source Code

I have put all codes in a zip file. You can download it by clicking on the Download button below. You do not need to register yourself to download it. You can directly use the code or you can modify them as per your requirements.

create a login system with remember me feature in phpConclusion

In this example I have showed you how you can develop PHP login form with "Remember Me" using PHP session cookie. This is a basic need for a web application where user login is used. I made it very simple for you, you can always improve it. Hope it will be useful for you.