PHP Login and Remember me Script using Cookie

I was viewing my google analytics account other day and I saw many searches of login with remember me script. Usually I wrote some planned tutorial for php beginners and I was thinking to write a tutorial on cookies but may be another time Login with remember me script is based on cookie. So first you need to know what is cookie. Wikipedia define it as:

An HTTP cookie (also called web cookie, Internet cookie, browser cookie, or simply cookie) is a small piece of data sent from a website and stored on the user’s computer by the user’s web browser while the user is browsing.

Cookie Syntax:

setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);

Parameter Description
$name The name of the cookie.
$value The value of the cookie
$expire The time the cookie expires. Must be in UNIX timestamp
$path The path on the server in which the cookie will be available on. If set to ‘/’, the cookie will be available within the entire domain
$domain The (sub)domain that the cookie is available to
$secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists.
$httponly When TRUE the cookie will be made accessible only through the HTTP protocol

Accessing Cookie:

Super global variable $_COOKIE is used to access cookie like $_COOKE[$name].

login with remember me

What I am going to do:

I will create a login form with email, password and remember me checkbox. If user will submit correct email and password with checked on remember me then I will create a cookie for email and password and create session and redirect user to dashboard.php page.

This tutorial is for beginner and my mainly focus is on cookie and remember me functionality for this i will use md5() function for password. md5() hashing algorithm is now obsolete it is recommend to use password hashing. Visit my post password_hash for registration and login form for better understanding.


PHP Login and Remember me:

Create Database:

Create MySQL Table:

Insert Record in Table:

I will use hash of admin123 as password.

Database Connection:

Login Form HTML:

Below is the simple login form. In input field there is a $_COOKIE super global variable in value which will check the value of cookie. If cookie value is already set then it print that value otherwise it will print nothing.

After Login Submit PHP Code:

When user will press submit button then below code will trigger. First i check email and password must have values then i use md5 for converting password. After that write a query and check user is exist on database or not and get num row. If user exists which means $nuRows is greater than zero. Then I fetch the record and store in $getRow.

$forOneHour is use to create cookie for 1 one hour and then I check the value of $_POST['remember_me'] which means if user checked on remember me checkbox then this value will be 1 and I will create cookies by using setcookie function. Otherwise $_POST['remember_me'] is not set and i wont set cookie. Just create session and redirect user dashboard.php page.

Dashboard.php Page

Dashboard.php has 2 conditions. One is for not accessing directly and second is for logout.

That’s all I hope now are able to create login with remember me script by own. Please note that we will not save secure information like password in cookies. But if we want then it is better to use any salt value or encryption for that.

 

Posted in PHP

Author: Ahsan Zameer

I am a web developer and love to search new stuff on web. Trying different approaches and ideas to make web developing more interesting and enjoyable.