Wednesday, 22 July 2015

PHP Cookies

A Cookie is usually accustomed established a user. A cookie could be a little file that server embeds on the  user’s PC. Each time a  similar PC request a page with a browser,it will send a cookie too. With a PHP, you’ll  each produce and receive cookie’s value.
  • Setcookie( )  function  is used to set cookie.
  • Setcookie() function must appear before the  <html> tag.

Syntax of cookie is:
setcookie(name, values, expiry, path, domain);
We take a example which makes easier to understand this:

HTTP/1.1 200 OK
Date: Fri, 25 Jan 2005 18:15:05 GMT
Set-Cookie:name=joy;expires=Sunday,25-Jan-09 10:33:45 GMT;
path=/; domain=fullyhelp.com

Retrieving a cookie:

PHP $_cookie  variable is used to retrieve a cookie value.

<?php
// Print a cookie
echo $_COOKIE[“joy”];
// The technique is used to view all  cookies
print_r($_COOKIE);
?>

Delete a Cookie:

Deleting a php cookies is very simple process, set the value of the cookie to null, and also set the expire time of the cookie in the past. Ideally, one would set the expiry time to about a year in the past, so that if the system time is off , then the cookie will still delete correctly.

<?php
// set the expiration date to one year ago
setcookie(“name”, “”, time()-365);
?>

No comments:

Post a Comment