Wednesday, 21 December 2016

Session And Cookies

SESSION

Session is logical object created by the PHP engine to allow us to preserve data across subsequent HTTP request.
  

  • Sessions are server-side files that contain user information, while Cookies are client-side files that contain user information.
  • Sessions have a unique identifier that maps them to specific users. This identifier can be passed in the URL or saved into a session cookie.
  • Most modern sites use the second approach, saving the identifier in a Cookie instead of passing it in a URL (which poses a security risk).
  • You are probably using this approach without knowing it, and by deleting the cookies you effectively erase their matching sessions as you remove the unique session identifier contained in the cookies.


E.g.
session_start();
$_SESSION['session-name']=$value;
unset($_SESSION['session_name']);

COOKIE

Cookie is a small piece of data stored on user's computer in user's browser while user is browsing a website.

They are designed to hold a modest amount of data specific to a particular client/website and can be accessed either by the web server or the client computer.

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

E.g. Cookie with expiry time 30 days from present
setcookie($cookie_name, $cookie_value, time()+86400*30, "/");                    // 86400 =1 day
Deletion -> unset($_COOKIE['your_cookie']);


No comments:

Post a Comment