Cookies - Create, Access, Delete

are like name tags;  You tell the server your name and it can give you a sticker to wear.    Then it can know who you are by referring back to that name tag.


  ex.: 


  setcookie('firstname', 'vins');

  setcookie('user_id', 'linux');


Access Cookies


To access use the $_COOKIE['value name'], along with the register_globals settings turn on in PHP.


  ex.: 


  setcookie('username', 'larry');

  $_COOKIE['username']


  the sample code below show's, how to check for an existing cookie and as follows:


  ex.: 


  IF ( !isset($_COOKIE['username'])) {


    header("location: http://".     $_SERVER['HTTP_HOST'] .     dirname($_SERVER['PHP_SELF']) .     "/index.php");

    exit();

  }


Delete or Remove a Cookie


ex.: 


  setcookie('firstname', '');

  setcookie('firstname', '', time()-300);



Cookie parameters


  syntax: 


  setcookie('name', 'value', expiration, 'path', 'domain', secure);


  ex.: 


  setcookie('name', 'value1', time()+3600);


  • time()+3600 - this will set the expiration time to one(1) hour or 60 mins. only, from the time of it's connection  
  • setcookie('name', 'value2', time()+3600, '/admin');
  • '/admin' - the path and domain arguments are used to limit a cookie to a specific folder or directory or with in a web site(the path) or a Host.