ITTreats.com
Enter your Search term and Hit "Enter"

Thu, 18 Mar 2010 5:44:00 AM +00:00

Latest 'ITTreats'


Using Cookies in PHP

Using Cookies in PHP

Using Cookies in PHP

cookie is a piece of information stored on to the users computer.We can set

timestamp for cookie which will stores the data till the date of expiry until

and unless user clear off the cookies.

in PHP we can create, reset, clear the cookies <br>

Creating Cookie

by using setcookie function we can create cookie

setcookie(name,value,expires,include_path,domain)

except nameĀ  all the remaining parameters are optional

name: name of the cookie

value : value of the cookie

exipres : timestamp or life span of the cookie

usually we will use time() function for this

ex: setcookie(&quot;username&quot;,&quot;supreme&quot;,time()+60*60*24)

&nbsp;&nbsp;&nbsp;&nbsp; time()+60*60*24 means from current time to next day

current time

&nbsp;&nbsp;&nbsp;&nbsp; time()+60*60*24*30 meansĀ  from current time to

next 30 days

You have to create cookie&nbsp; before the html content loads to the

browser,Once if HTML content start loading then&nbsp; setcookie will throw

warning saying headers are already sent from ….

so,its better to use this on the top of the page or before HTML content

Retrieving Cookie

using $_COOKIE we can retrieve the data of the cookie

ex: echo “User Name is :”.$_COOKIE[&quot;username&quot;];

Delete Cookie

To Delete the Cookie we will use the same setcookie, but we have to supply

past time stamp means&nbsp; time()-60*60

ex: setcookie(“username”,”",time()-60*60);