Latest 'ITTreats'
- Microsoft Tech
- SUN Tech
- Open Sources
- Databases
- Software Testing
- Others
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("username","supreme",time()+60*60*24)
time()+60*60*24 means from current time to next day
current time
time()+60*60*24*30 meansĀ from current time to
next 30 days
You have to create cookie before the html content loads to the
browser,Once if HTML content start loading then 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["username"];
Delete Cookie
To Delete the Cookie we will use the same setcookie, but we have to supply
past time stamp means time()-60*60
ex: setcookie(“username”,”",time()-60*60);




