P. 1
Fast Track to PHP

Fast Track to PHP

|Views: 91|Likes:
Published by Raghavan Mohan
Think Digit : Fast Track to PHP
Think Digit : Fast Track to PHP

More info:

Published by: Raghavan Mohan on Oct 11, 2010
Copyright:Traditional Copyright: All rights reserved


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less


In PHP, a cookie is set in the following ways:

setcookie() function:As the name suggests, setcookie() outputs a
header and is used before sending any content to the browser. The
function has certain optional and essential attributes. Cookie
value, expiry date in UNIX epoch format, domain, path and inte-
ger are the optional attributes. Only the cookie name is the essen-
tial attribute of the setcookie() function.

$cookie_val =”We are testing cookies”;
setcookie(“OurCookie”, $cookie_val);
setcookie(“OurCookie”, $cookie_val, time()+3600);
if (isset($_COOKIE[‘OurCookie’]))
echo $_COOKIE[“OurCookie”];

Here, either statement in line 2 or line 3 can be used to set the
cookie. The value assigned to a cookie variable can also be accessed
from other pages. The example given below displays the cookie
created in the above example:

echo $_COOKIE[“OurCookie”];
echo $HTTP_COOKIE_VARS[“OurCookie”];

Saving state in PHP


Cookies can also be set in array as given below:

setcookie(“owncookie[a]”, “First Cookie”);
setcookie(“owncookie[b]”, “Second Cookie”);
setcookie(“owncookie[c]”, “Third Cookie”);
if (isset($_COOKIE[‘owncookie’])) {
foreach ($_COOKIE[‘owncookie’] as $cookiename =>

$Ourcookie) {

echo “$cookiename : $Ourcookie

print_r($_COOKIE);can be used to display all the available


You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->