For storing and get data web browser need some mechanism of state management process. So PHP Session and PHP Cookie help us to manage the states. As we know that https is stateless that means it don’t maintain the data of previous or current request by user.It only help us to load html , images and related files from the server on website.

PHP Session:

Session state is a server-based state component that lets web applications store and recover objects of any kind for every one of a kind client session. Session state is perfect for putting away increasingly complex objects or data structures that are related with a client session

PHP Session variable $_SESSION which is known as Super Global Variable.For staring PHP session we have to write session_start() at the starting of Page, So that PHP will open and save handlers to manage session

Set Session:

<?php session_start(); $_SESSION['name']="John"; $_SESSION['cart']=["Mobile","Book","Pen"]; ?>

Destory Session:

Commonly session is destroyed when the user when to logout from website.

<?php session_destroy(); echo "You have successfully logout!!" ?>

Working:

A new session is created, PHP assign a new collection in which session state can be managed.

is created, assign a new collection in which session state can be managed. Whenever the request of saving session is finished, session are save in storing mechanism called session state provider.

When we close the all tabs of a website than all the session related to the website are destroyed. So web browser can’t save the session in its cookie,history etc.

PHP Cookies:

Cookies is the mechanism of storing data on client-side (web browsers). PHP cookies have information as key-value pair.

Working:

In cookie the information travels through HTTP header.So information is manage by the web browser. As every website has access to it ,So it can be easily hack by any website.We can also delete it from web browser

Types:

Persistent cookie which has some expiry date.It will destroyed after that date.

which has some expiry date.It will destroyed after that date. Session cookie which don’t have expiry date,it is destroyed when user closed the web browser.

Syntax:

setcookie(key, value, expire time/date, path, domain, secure, http);

Set Cookie:

<?php $name="Username"; $password="12345678"; $Time=time()+60*60*24; // expiry time for a day setcookie($name,$password,$Time); ?>

Recover Data from Cookie:

<?php $pass="12345678"; if(isset($_COOKIE['John'])){ if($_COOKIE['John']==$pass){ echo "Successfully Login"; }else{ echo "Password is incorrect!"; } } ?>

Output:

Successfully Login