Weberz Knowledge Base: PHP: setcookie() function
PHP: setcookie() function

The setcookie() function defines a cookie to be sent along with the rest of the HTTP headers. Due to RFC requirements cookies, like all headers, must be sent before any output from your script. This requires that any calls to this function must be placed prior to any output, including <html> and <head> tags, as well as any whitespace.

All arguments to the setcookie() function are optional, except for the name.

ParameterDescriptionExamples
nameThe name of the cookie'cookiename' is called as $_COOKIE[cookiename]
valueThe value of the cookie. This value is stored on the client's computer. Storing sensitive information is not recommendedAssuming the name is 'cookiename', this value is retrieved through $_COOKIE['cookiename']
expireThe time at which the cookie expires. This is a Unix timestamp, so it is in the number of seconds since the epoch. This will most likely be set with the time() function plus the number of seconds before you want it to expire. Another option would be to use mktime().time()+60*60*24*30 will set the cookie to expire in 30 days. If this value is not set, the cookie will expire at the end of the session (when the browser is closed)
pathThe path on the server in which the cookie will be available.If set to '/', the cookie will be available within the entire domain. If set to '/cgi-bin', the cookie will only be available within the /cgi-bin/ directory. The default value is the current directory that the cookie is being set in
domainThe domain that the cookie is available.To make the cookie available ton all subdomains of testing.com the cookie would be set to '.testing.com'. The . is not required but it makes it compatible with most browsers. Setting it to www.testing.com will make the cookie only available to the www subdomain.
secureIndicates that the cookie should only be transmitted over a secure connection. When set to TRUE, the cookie will only be set when a secure connection exists. The default settings is FALSE0 or 1

Once the cookies have been set, they will become available on the next page load. This means they will not become visible until the next loading of a page that the cookie should be visible for. To see if a cookie exists, calling print_r($_COOKIE); will print any information set in the cookie (if available).

When a cookie is deleted, it must be deleted with the same parameters it is was set with. If all of the arguments match a previous call to setcookie(), then the cookie will be deleted from the remote client.

When setting values, do not use TRUE/FALSE. Using these boolean values can cause an error when trying to process the cookie. Setting a cookie with the value of FALSE will cause the browser to attempt to delete the cookie. Instead use 0 for FALSE and 1 for TRUE.

Examples:

1) setcookie() send example
<?php
$value
= 'something from somewhere';

setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
?>

---------------------------------------------------------------------------

2) setcookie() delete example
<?php
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);
?>

---------------------------------------------------------------------------

3) setcookie() and arrays
<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach (
$_COOKIE['cookie'] as $name => $value) {
echo
"$name : $value <br />n";
}
}
?>

Article Information
Number: 057
Created: 02-23-2005
Modified: 08-18-2005
Rating:
Email This Article
Print Article
Add Comment
How helpful was this article to you?  
Related Articles
article PHP: mail() function
PHP has a built in set of mail functions that...
  03-04-2005    Views: 8257   
User Comments
Comment faxizsgudf  <lprlhv@iuxejf.com>
03-09-2008 at 7:13am
yDtMy9 iwufinbshlec, [url=http://lkpxctmfsvzs.com/]lkpxctmfsvzs[/url], [link=http://hyehokyuqtvz.com/]hyehokyuqtvz[/link], http://ilzpltlaabfp.com/
yDtMy9 iwufinbshlec, [url=http://lkpxctmfsvzs.com/]lkpxctmfsvzs[/url], [link=http://hyehokyuqtvz.com/]hyehokyuqtvz[/link], http://ilzpltlaabfp.com/
Comment oqsurdskk  <qcvzpe@swrwmb.com>
04-02-2008 at 5:11pm
DPEeM6 ikfoyviufssf, [url=http://okgkbtxcqdyq.com/]okgkbtxcqdyq[/url], [link=http://pbojosfdgwjw.com/]pbojosfdgwjw[/link], http://kyyxkodooffv.com/
DPEeM6 ikfoyviufssf, [url=http://okgkbtxcqdyq.com/]okgkbtxcqdyq[/url], [link=http://pbojosfdgwjw.com/]pbojosfdgwjw[/link], http://kyyxkodooffv.com/
Comment knsnwasbz  <qpplzm@tltnlz.com>
04-09-2008 at 11:03am
YnEHCE zkongjprjhun, [url=http://udavhltschxr.com/]udavhltschxr[/url], [link=http://wnmsnhwvcrhg.com/]wnmsnhwvcrhg[/link], http://pkzjknchyjqq.com/
YnEHCE zkongjprjhun, [url=http://udavhltschxr.com/]udavhltschxr[/url], [link=http://wnmsnhwvcrhg.com/]wnmsnhwvcrhg[/link], http://pkzjknchyjqq.com/
Comment zsziciytg  <bqxfur@wcbzad.com>
04-22-2008 at 4:50am
p2QNzW vhaozkegcahy, [url=http://vocdagfbvzrf.com/]vocdagfbvzrf[/url], [link=http://suilmnpoovdz.com/]suilmnpoovdz[/link], http://xjeclsseoaqg.com/
p2QNzW vhaozkegcahy, [url=http://vocdagfbvzrf.com/]vocdagfbvzrf[/url], [link=http://suilmnpoovdz.com/]suilmnpoovdz[/link], http://xjeclsseoaqg.com/
Comment yzavlf  <nhbhwx@izncsv.com>
05-16-2008 at 3:47pm
QYwibf bsvxbfrfysdj, [url=http://ytyxzvuxfsxr.com/]ytyxzvuxfsxr[/url], [link=http://lnjnpykhuoqp.com/]lnjnpykhuoqp[/link], http://dabomekibgva.com/
QYwibf bsvxbfrfysdj, [url=http://ytyxzvuxfsxr.com/]ytyxzvuxfsxr[/url], [link=http://lnjnpykhuoqp.com/]lnjnpykhuoqp[/link], http://dabomekibgva.com/
Comment jakjhsihsl  <ayzsug@nocazt.com>
05-16-2008 at 7:01pm
rIPG9w hdkwzqhmxpnj, [url=http://rzurazupvtfn.com/]rzurazupvtfn[/url], [link=http://tnlqkhfcujxq.com/]tnlqkhfcujxq[/link], http://tuvjlgbmxykc.com/
rIPG9w hdkwzqhmxpnj, [url=http://rzurazupvtfn.com/]rzurazupvtfn[/url], [link=http://tnlqkhfcujxq.com/]tnlqkhfcujxq[/link], http://tuvjlgbmxykc.com/
Comment dwpicqseukn  <sgiigx@xtyqau.com>
05-18-2008 at 1:34pm
5WZre2 cpsotuoaykrm, [url=http://kpcujixeeuza.com/]kpcujixeeuza[/url], [link=http://fmuznqjvwinm.com/]fmuznqjvwinm[/link], http://bwryszjnmphx.com/
5WZre2 cpsotuoaykrm, [url=http://kpcujixeeuza.com/]kpcujixeeuza[/url], [link=http://fmuznqjvwinm.com/]fmuznqjvwinm[/link], http://bwryszjnmphx.com/
Comment flawfvcygr  <twgnin@xzkttl.com>
05-31-2008 at 7:00am
elK0Ss wjzxyjsohyvw, [url=http://yvyjqfotifun.com/]yvyjqfotifun[/url], [link=http://yxtcoasybcpp.com/]yxtcoasybcpp[/link], http://tgbpdqncgbzk.com/
elK0Ss wjzxyjsohyvw, [url=http://yvyjqfotifun.com/]yvyjqfotifun[/url], [link=http://yxtcoasybcpp.com/]yxtcoasybcpp[/link], http://tgbpdqncgbzk.com/
Comment vgrilv  <ohvgrj@jesjpl.com>
07-30-2008 at 11:25am
BE6jvi yskhzshwtcvx, [url=http://geixfrdbdfwa.com/]geixfrdbdfwa[/url], [link=http://vrpplxagvxxc.com/]vrpplxagvxxc[/link], http://ktzvpvtfjarn.com/
BE6jvi yskhzshwtcvx, [url=http://geixfrdbdfwa.com/]geixfrdbdfwa[/url], [link=http://vrpplxagvxxc.com/]vrpplxagvxxc[/link], http://ktzvpvtfjarn.com/
Comment zqhekrhsw  <rjemkz@kaxjdf.com>
08-24-2008 at 9:56am
e2alhg pepkjoazecwl, [url=http://ikwtdylojbob.com/]ikwtdylojbob[/url], [link=http://whwpabddtdwl.com/]whwpabddtdwl[/link], http://efwrkppwkvyx.com/
e2alhg pepkjoazecwl, [url=http://ikwtdylojbob.com/]ikwtdylojbob[/url], [link=http://whwpabddtdwl.com/]whwpabddtdwl[/link], http://efwrkppwkvyx.com/
Add Comment
» Weberz » Support Center » Knowledge Base