Difference between a Session and a Cookie?


99knowledgehub-Session-Cookie


A session is a global variable stored on the server side. Each session is assigned a unique id which is used to retrieve a  stored values.

Ex-First start session

<?php
session_start();
$_SESSION["name"]=$_POST["username"];
?>

• Sessions have the volume to store comparatively large data compared to cookies. The session values are automatically deleted when the browser is closed then session is expired.
• Session data is stored on the server side, whereas cookies store data in the visitor's browser side.
• Sessions are more secure than cookies as it is stored in server. Cookie can be turn off from browser.
Ex- Cookie start with

Syntax: setcookie(name, value, expire, path, domain); 

<?php

$xyz=time()-60*60*24*30; //  Sec*Mint*Hours*Day
setcookie("Mohit"," Bca 1st Year",$xyz);
echo $_COOKIE['Mohit'];
print_r($_COOKIE);

?>

Session:

Session variables stored information about one single user, and are available to all pages in one application.

<?php
session_start();
// store session data
$_SESSION['username']=$_POST['xyz@gmail.com'];
?>
<?php
echo "Username=". $_SESSION['username'];
?>

Destroying a Session-
Using two function unset () or the session_destroy() .
The unset() function is used delete some session data:

<?php
session_start();
if(isset($_SESSION['username']))
  unset($_SESSION['username']);
?> 
You can also completely destroy the session by calling the session_destroy() function:
<?php
session_destroy();
?> 

Cookie:


A cookie is a text file saved to a users system by a website.

Important Security Features-

  • A cookie can only be read by the website or domain that created it.
  • A single domain can’t set more than 20 cookies.
  • A manimum number of cookies can’t exceed 4kilobytes size .
  • A maximum number  of cookies that may be set on a user’s syatem 300.

Syntax-
Set cookie(cookiename,value,expire,path,domain);

<?php
setcookie("99knowledgehub", "blogspot",time()+60*60);
$expire=time()+60*60;
setcookie("99knowledhgehub", "blogspot", $expire);
?>

Get_cookie-
<?php

echo $_COOKIE["99knowledgehub"];

?>

Retrive all cookies-

<?php
print_r($_COOKIE);
?>

DELETE COOKIE-
<?php
setcookie("99knowledgehub", "blogspot",time()-60*60);
?>


Comments

Popular posts from this blog

What is Permalinks How to Reset permalinks ?

WHY will we want DIGITAL MARKETING?

How to Create Database Using Mysql,Mysqli,PDO or using Oops concepts in php?