PHP setcookie()Functions


PHP HTTP 参考手册Complete PHP HTTP Reference Manual

Definition and Usage

The setcookie() function sends an HTTP cookie to the client.

A cookie is a variable sent from the server to the browser. A cookie is usually a small text file that the server embeds on the user's computer. This cookie is sent whenever the same computer requests a page through a browser.

A cookie is automatically assigned a variable with the same name. For example, if a cookie named "user" is sent, a variable named $user is automatically created containing the cookie's value.

The cookie must be set before any other output is sent to the client.

Returns TRUE on success, or FALSE on failure.

Syntax

setcookie(name,value,expire,path,domain,secure)

Parameters Description
name Required. Specifies the name of the cookie.
value Required. Specifies the value of the cookie.
expire Optional. Specifies the expiration time of the cookie.

time()+3600*24*30 will set the cookie to expire in 30 days. If this parameter is not set, the cookie will automatically expire when the session ends (i.e., when the browser is closed).

path Optional. Specifies the server path of the cookie.

If the path is set to "/", the cookie will be valid throughout the entire domain. If the path is set to "/test/", the cookie will be valid in the test directory and all its subdirectories. The default path value is the current directory where the cookie is located.

domain Optional. Specifies the domain of the cookie.

For the cookie to be valid in all subdomains of example.com, you need to set the cookie's domain to ".example.com". When you set the cookie's domain to www.example.com, the cookie is only valid in the www subdomain.

secure Optional. Specifies whether a secure HTTPS connection is required to transmit the cookie. Set to TRUE if the cookie needs to be transmitted over a secure HTTPS connection. Default is FALSE.


Tips and Notes

Tip:You can access the value of a cookie named "user" via $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"].

Note:When sending a cookie, the cookie's value is automatically URL-encoded. It is automatically URL-decoded when received. If you do not need this, you can use setrawcookie() instead.


Example 1

Set and send a cookie:

<?php
$value = "my cookie value";

// send a simple cookie
setcookie("TestCookie",$value);
?>

<html>
<body>

...
...

<?php
$value = "my cookie value";

// send a cookie that expires in 24 hours
setcookie("TestCookie",$value, time()+3600*24);
?>

<html>
<body>

...
...


Example 2

Different ways to retrieve a cookie value (after the cookie has been set):

<html>
<body>

<?php
// Print individual cookies
echo $_COOKIE["TestCookie"];
echo "<br />";
echo $HTTP_COOKIE_VARS["TestCookie"];
echo "<br />";

// Print all cookies
print_r($_COOKIE);
?>

</body>
</html>

The code above will output:

my cookie value
my cookie value
Array ([TestCookie] => my cookie value)


Example 3

Delete a cookie by setting its expiration date to a past date/time:

<?php
// Set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
?>

<html>
<body>

...
...


Example 4

Create an array cookie:

<?php
setcookie("cookie[three]","cookiethree");
setcookie("cookie[two]","cookietwo");
setcookie("cookie[one]","cookieone");

// print cookies (after reloading page)
if (isset($_COOKIE["cookie"]))
{
foreach ($_COOKIE["cookie"] as $name => $value)
{
echo "$name : $value <br />";
}
}
?>

<html>
<body>

...
...

The code above will output:

three : cookiethree
two : cookietwo
one : cookieone


PHP HTTP 参考手册Complete PHP HTTP Reference Manual Other Extensions