Successfully reported this slideshow.
Your SlideShare is downloading. ×

Cookie and session

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
PHP Cookies and Sessions
PHP Cookies and Sessions
Loading in …3
×

Check these out next

1 of 14 Ad
Advertisement

More Related Content

Advertisement
Advertisement

Cookie and session

  1. 1. COOKIE AND SESSION IN PHP By: Aashish Ghale aashish.ghale@gmail.com
  2. 2. COOKIES Like variable  Stores single piece of data under a unique name  Browser dependent   stores cookies on user‟s browser Moves page to page  Avoids the problem to log in every time  Always stored as text 
  3. 3. COOKIES WITH PHP  Set cookie setcookie() stores data in cookies  Syntax: setcookie(“cookie name”,”value”);  Accepts an optional third argument    E.g: setcookie(„username‟, „aacssh‟, time()+(60 * 60));   Sets the expiration date of the cookie Sets expiration date 1 hour (60sec * 60min) from the current time Retrieve cookie  using super global using $_COOKIE  E.g: $_COOKIE[„cookie name‟];
  4. 4. (CONT…)  Delete cookie Set expiration date to time in the past Syntax:  setcookie(„username‟,‟aacssh‟, time()-3600);  Set time in the past
  5. 5. SESSION Time spend by user at web application  Stores small piece of data on server  More secure and more reliable  Automatically destroyed by shutting down the browser  Stored in session variable 
  6. 6. SESSION WITH PHP  Starting session  Started by function session_start() Allow data to store in session variables  Should be the first line of code   End session  Close session with session_destroy()
  7. 7. (CONT…)  Session variables  Set session variable with super global $_SESSION  Once created , gets stored on the server  E.g: $_SESSION[„variable‟] = value;  Variable = name of session  Delete session variables manually  Not automatically deleted when session is destroyed  Set $_SESSION superglobal to an empty array  E.g: $_SESSION = array();
  8. 8. HOW PHP SESSION WORKS
  9. 9. USING BOTH SESSION AND COOKIE  Sessions have short life  Cookie can last forever   Long enough to outlast a session If session uses cookie to remember session id  Cookie id is name after the session   i.e session_name() To close session, cookie must also be deleted
  10. 10. SOMETHING MORE…..  Header function Tells the server to send a new page  Syntax: header(“Location: URL”); 
  11. 11. CODE SNAPSHOTS : 1. Combining session and cookie 1. Creates session and cookie
  12. 12. CONTD…  session.php file  Separate file that is required in every php file.
  13. 13. CONTD  Logout.php  Destroies sessions and cookies
  14. 14. Lets make a simple login system

×