PHP Cookies

A cookie is used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, we are going to create and retrieve cookie values.

In PHP, setcookie () function is used to set a cookie.

Function:  setcookie ()
setcookie(name, value, expire);
When we create a cookie, using the function setcookie, We must specify the following  arguments.

  1. name: The name of your cookie. This name will use  later to  retrieve your cookie
  2. value: The value that is stored in your cookie.
  3. expiration: The date when the cookie will expire and be deleted.

Note: The setcookie() function must  appear before  <html> tag.
Creating a cookie:
{code codetype=php}<?php
setcookie(“user”, “Username”, time()+3600);
?>
<html>{/code}
Retrieving  a cookie:
{code codetype=php}<?php
echo $_COOKIE["user"];
print_r($_COOKIE);
?>{/code}
Deleting a cookie:
{code codetype=php}<?php
// set the expiration date to one hour ago
setcookie(“user”, “”, time()-3600);
?>{/code}
Reference: w3schools.com

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.17_1161]
Rating: 0 (from 0 votes)

Related Posts

  • No Related Post
Related Posts with Thumbnails

About the Author

CoreTimer

has written 31 stories on this site.

System administrator(Linux/UNIX/Windows) RHCE,MCP,CCNA

Write a Comment

Copyright © 2012 CoreTimer.com|System Admin’s Reference. All rights reserved.

Switch to our mobile site