SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP Declaring Variables

PHP creating cookies setting and deleting

Cookies are used by web sites to store information about the visitor at the hard disk of the user computer through the browser. The difference between session and cookies is the cookies are stored at client computer and the sessions are stored at server side. The user have full control over the cookies it want to store or not and for what level it wants to store. The browser setting for the cookies can be managed by going to

Tools >> Internet Options >> Privacy then advanced button.

Unlike sessions cookies are managed by users through its web browser so we have to carefully select which data to be used through cookies.

Sending cookies by setcookie

Related Tutorial
PHP Session
We will study how to set cookies in the client computer through the browser by using PHP. Cookies are stored with some values in it and they can be read by other files in the same or down below directory. We will see how to set the cookies first. You can download the free code at the end of this page for your testing. You can use the demo here also. Here is the code to do that.

$value=”Plus2net Tutorials”;
setcookie ("Plus2netCookie", $value,time()+3600); /* expire in 1 hour */

The above line will set the cookie with the variable value set to a string. This will expire after one hour as the time of expire is set to one huour ( +3600 seconds ) than the present time. We have used Unix time stamp to get the present time value and then added the expire time of the cookies in seconds.

setcookie ("Plus2netCookie", $name,time()+60*60*24*30);// Expire in 30 days

Two things we have to know here.
1. Cookie set by the page now will be available at next page.
2. Before sending any header to the browser ( before sending any html code ) the cookie has to be set. Otherwise error message we will get.

Reading cookies

To read the cookie we can check the value as a variable or by using the code below.

echo "Welcome " .$HTTP_COOKIE_VARS["Plus2netCookie"];

In PHP 5 and above it is

echo "Welcome " .$_COOKIE["Plus2netCookie"];

Delete Cookies

Same way to delete the cookies we will set the same cookie again with expire time one hour before by using time()-3600 . That will delete the cookie from the computer.

setcookie ("Plus2netCookie", "", time() - 3600);

This will delete the cookie. Here is the demo of this script.

Enter Your Name



Start a new cookie cookies_t1.php | Check the cookie cookies_t2.php | Remove the cookie cookies_t3.php


Download the ZIP file here  cookies.zip

Number of User Comments : 2


Google+
gsdfg20-07-2012
hi very useful this code
Opeyemi Orisatoberu04-01-2013
This post is very useful... Tnx...
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer