PHP: Login with Cookies | Login cookie Example - Mini Project..

Techno PHP

The most important part of any website is login system . Login system is done by two things.
1. Session 2. Cookies.

We will learn about how to log in with cookies in PHP ?

What is cookie ?

Cookies are just like a information stored in a web browser. Cookies depend on client side not on server side.
In simple word If you have a login then after 5 days you log in again but this time you do not require to fill up any id and password and you got you are login directly and when you return after 1 month then you try to log in and cookies have expired that means cookies set to 1 month expiry time .

After 1 month you can not access the cookie. so It’s all done through the cookies. We will set a simple code for a mini project on cookie .

The pages we will include –

1. Index.php
2. Login.php
3. Loginprocess.php
4. Page1.php
5. Page2.php
6. Page3.php
7. Logout.php

First of all we will set a database for login process . The Database Structure –


CREATE TABLE  `users` (

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


Now we will set a Home Page(index.html) page.
In index.html we will check the if the cookie is not set. If the cookie is not set then you can not access the page and redirect to 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

In the Login page we will take a HTML form for login .
1. Form field .


2. Check the password valid or not through the if condition.

3. If username or password doesnot exists then display the message Invalid username or Password. (validation).

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>



The loginprocess.php file will handle the part of database base connectivity and login process .
Step by step process-


1. Database connectivity .

2. If username and password is set .

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

4.Set the cookies for 60 seconds on web page .

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");
	
}
}
?>

We will include the page 1 ,page2 ,and page 3 as example to access . In the all pages(page 1 ,page 2 and page 3 ) we will include a condtion . If the cookie id not set then redirect to login page (login.php) .

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

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

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

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 Cookie)

With help of setcookie, we can also delete the cookie . To delete a cookie you need setcookie() function as well as with its name and arguments.

Logout.php

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