Wednesday, 1 February 2017

COOKIES IN PHP

Cookie is a star management object used to maintain state of application. Data of cookie will be stored in browser memory location. This data can be accessed from any web page in application. Cookie is divided into two types –

1. In-memory cookie – If we create a cookie in client system without explicit expiry time, comes under in-memory cookie. Data of this cookie will delete after closing web browser.

2. Persistence cookie – Cookie with explicit expiry time comes under persistence cookie. This cookie stores data in hard disk memory location, that data will destroy after its lifetime is completed


SetCookie – Using this function, we can create cookie in client system.


$_COOKIE – Using this super global variable we can access data of cookie.

Example –

p1.php
<?php
SetCookie('x',100);
echo $_COOKIE['x'];
?>
<a href = "p2.php">Go</a>

p2.php
<?php
echo "hi";
echo $_COOKIE['x'];
?>

Note – At the time of first execution, the value of x is not shown in the output. Since the time SetCookie() is creating the cookie, the next statement is executed. This SetCookie() takes some time to create cookie, thus we are unable to see value of x at first execution.

Every browser contains memory locations in RAM and Hard-disk. Hard disk memory allocates when the browser is installed. RAM memory allocates when we open the first window browser.

RAM memory deallocates when we close all windows of browser. Hard disk memory deallocates after uninstallation of browser.

Memory of browser cannot be accessible by another browser.

All windows of a browser can share common memory location.

Every website contains a folder in browser. Cookies of that website will store in that folder, that’s why cookies of one website, cannot be accessed from another website. This folder will be created when website is opening in browser.


To view cookie in browser

Browser----setting----show advanced setting----privacy----content setting----All cookies and site data    

Note – The above steps is applicable for Chrome, might be changed for other browser.


Steps to create Persistence Cookie

1. Find out current date and time information when cookie is downloading into client system.
2. Add lifetime to current date and time to get expiry time.
3. Create cookie in client system with resultant expiry time.
<?php
SetCookie("name","cmp",time()+3600);
echo $_COOKIE['name'];
?>
4. We can destroy the cookies before its expiry time by recreating the cookie with completed time.
<?php
SetCookie("name","",time()-3600);
?>


Limitations of Cookie

1. Cookie can store limited amount of data.
2. Cookie can store only text data.
3. Data of cookie gets stored in browser memory location, that’s why we should not store secure information in cookies. If we want to store secure data, we should get user’s permission.

Example –

p1.php
<?php
if(isset($_POST['submit']))
{
          $product=$_POST['mobile'];
          $qty=$_POST['qty'];
          SetCookie($product,$qty);
}
?> 
<form method="post" action="">
          Select Product : <select name="mobile">
          <option>Apple</option>
          <option>MI</option>
          <option>Nokia</option>
          </select><br>
          Enter Quantity : <input type="text" name="qty"><br>
          <input type="submit" name="submit" value="Add to Cart">
</form>
<a href="p2.php">Bill</a>

p2.php
<?php
foreach($_COOKIE as $k=>$v)
{
          echo $k." = ".$v;
          echo "<br>";
}

?>

No comments:

Post a Comment