Login system with cookies using PHP and MYSQL database


What is cookie ?

Cookie is a small file that is stored on the client computer. Cookie depends on the client-side, not on the server-side. In simple words, a small file stored on the client computer through the server. The cookie is not stored on the server. In other words, If you log in after 5 days, you do not require to fill up any id and password. The user is logged in without entering a username and password. We can set a cookie expiry time for 1 month or more. If we set 1 month. After 1 month you can not access the cookie. so It’s all done through the cookies. In the previous tutorial, we discussed the cookies in php. Here, we create a complete login system using PHP and MySQL database.

Create an MYSQL database table using query

We have to create a MySQL database for the login system using cookies. In the MySQL database, we create table, through which we create the login system using PHP and MySQL database. Let's create a table using query.


CREATE TABLE  `users` (

 `id` INT( 50 ) NOT NULL ,
 `uname` VARCHAR( 40 ) NOT NULL ,
 `upassword` VARCHAR( 40 ) NOT NULL
) ENGINE = INNODB DEFAULT CHARSET = latin1;

Insert data into the database table for login with cookies using PHP

Insert the data into the table. We can use the registration form or MYSQL insert query. When you create a login form, you have to fetch the data. Unless you have data in the database, you will not be able to create the login system using cookies. You can use the registration form to insert data or you can perform insert query to insert data into the MYSQL database table. Let's insert the data into database table using MYSQL query.

INSERT INTO `users` (`id`, `uname`, `upassword`) VALUES (1, 'admin', 'admin@123');

Create a connection file

The config file hanles the connection between PHP form and MYSQL database. Now, create a file and save with name and extension "config.php".


<?php 
$databaseHost = '127.0.0.1';//or localhost
$databaseName = 'project'; // your db_name
$databaseUsername = 'root'; // root by default for localhost 
$databasePassword = '';  // by defualt empty for localhost

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
 
?>

In above config file , we used mysqli_connect() function for a new connection. We passed these information as parameters and stored in a variable $mysqli. You can change database host, database name, database username, and database password according to your need.

Create a HTML login form included PHP code

Now, we create an HTML form. The user fills the username, password, and clicks on the login button. This is the starting page of login with cookies. In this HTML form, we create two input box and a submit button. First input box for username and a second input box for password. Here, we use if condition . If a username or password does not exists then display the message Invalid username or password.

login.php

<html>
<fieldset> 
<legend>Login </legend>
<form action="loginprocess.php" method="POST">

Username:

Password:

keep me login:
<?php if(isset($_REQUEST["err"])) $msg="Invalid username or Password"; ?>

<?php if(isset($msg)) { echo $msg; } ?>

</form> </fieldset> </center> </html>

Create a PHP script for login system with cookies

Now, we create a PHP script for form handling. The PHP code contains the form data and MYSQL query. The loginprocess.php file handles the part of database base connectivity and login process.
1.Include config file at the beginning of page.We can use PHP include() function and PHP require() function to include and require a config file.
2.Use if condition and set the form values on button.

3. Select query for match the username and password from the database if username and password are not matched then return to the login page and if username and password are matched then access the pages within the time duration.

4.Set the cookie for 60 seconds. After 60 seconds, the user will be logged out. In the previous tutorial, we discussed completely PHP cookies.

5. Use header() function after login success or not. If the login succcess then redirect to the index page. If the login does not success then redirect to the same login page.

6. You do not need to start cookies in every single page like session.

loginprocess.php

<?php

$cser=mysqli_connect("localhost","root","","project") or die("connection failed:".mysqli_error());

if(isset($_REQUEST['sub']))
{
$a = $_REQUEST['uname'];
$b = $_REQUEST['upassword'];

$res = mysqli_query($cser,"select* from users where uname='$a'and upassword='$b'");
$result=mysqli_fetch_array($res);
if($result)
{
	if(isset($_REQUEST["remember"]) && $_REQUEST["remember"]==1)
	setcookie("login","1",time()+60);// second on page time 
else
	setcookie("login","1");
	header("location:index.php");
	
	
}
else
{
	header("location:login.php?err=1");
	
}
}
?>

Make the main page after login success

Now, we create a Home Page(index.html) page. On the home page, we check if the cookie is not set. If the cookie is not set then you can not access the page and redirect to the login page . and if the cookie is set then you can access any kind of page of the website.

index.html

<?php 

if(!isset($_COOKIE["login"]))// $_COOKIE is a variable and login is a cookie name 

	header("location:login.php"); 

?>

Hey ! welcome to main page .

<a href="page1.php">

PAGE 1

<a href="page2.php">

PAGE 2

<a href="page3.php">

PAGE 3

<a href="logout.php">

Logout

Create another page 1 to access the cookie

A website contains lot of pages.Here,you find the another pages example. You can create another page of the website like this. Create another pages.

We create the page 1 ,page2 ,and page 3 as to access cookies . In these pages(page 1 ,page 2 and page 3 ) we include a condtion . If the cookie is not set then redirect to login page (login.php) and if cookie is set then stay on same page.

Let's create another pages.

Page1.php

<?php 
if(!isset($_COOKIE["login"]))
	header("location:login.php");
?>

Welcome to page 1

<table> <a href="page1.php">

PAGE 1

<a href="page2.php">

PAGE 2

<a href="page3.php">

PAGE 3

<a href="logout.php">

Logout

Create another page 2 to access the cookie

Here,we create another page 2 and access the cookie.

Page2.php

<?php 
if(!isset($_COOKIE["login"]))
	header("location:login.php");
?>

Welcome to page 2

<table> <a href="page1.php">

PAGE 1

<a href="page2.php">

PAGE 2

<a href="page3.php">

PAGE 3

<a href="logout.php">

Logout

Create another page 3 to access the cookie

Create another page 3 and access cookie. If cookie is not set or deleteted the redirect to login page otherwise stay on page.

Page3.php

<?php 
if(!isset($_COOKIE["login"]))
	header("location:login.php");


?>

Welcome to page 3

<table> <a href="page1.php">

PAGE 1

<a href="page2.php">

PAGE 2

<a href="page3.php">

PAGE 3

<a href="logout.php">

Logout

Logout | delete the cookies

Logout is a process of delete cookies. After delete cookies, unknown users cannot access the page. You can use setcookie() function and expiry time. This is known as destroyed cookies. Delete cookies by secookie() function .

Logout.php

<?php
setcookie("login","",time()-1);//for delete the cookie //destroy the cookie 
header("location:index.php")
	
?>

Please Share

Recommended Posts:-