Latest web development tutorials

PHP setcookie () function

PHP HTTP Reference Manual Complete PHP HTTP Reference Manual

Definition and Usage

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

A cookie is sent from the server to the browser variables. A cookie is often embedded in the server to the user's computer in a small text file. Whenever the same computer through a browser requests a page, it will send the cookie.

cookie name is automatically assigned to a variable of the same name. For example, if the cookie is sent, called "user", it will automatically create a variable named $ user, including the value of the cookie.

You must be sent before any other output to the client cookie assignment.

If successful, the function returns TRUE. If it fails it returns FALSE.

grammar

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

参数 描述
name 必需。规定 cookie 的名称。
value 必需。规定 cookie 的值。
expire 可选。规定 cookie 的过期时间。

time()+3600*24*30 将设置 cookie 的过期时间为 30 天。如果这个参数没有设置,那么 cookie 将在 session 结束后(即浏览器关闭时)自动失效。

path 可选。规定 cookie 的服务器路径。

如果路径设置为 "/",那么 cookie 将在整个域名内有效.如果路径设置为 "/test/",那么 cookie 将在 test 目录下及其所有子目录下有效。默认的路径值是 cookie 所处的当前目录。

domain 可选。规定 cookie 的域名。

为了让 cookie 在 example.com 的所有子域名中有效,您需要把 cookie 的域名设置为 ".example.com"。当您把 cookie 的域名设置为 www.example.com 时,cookie 仅在 www 子域名中有效。

secure 可选。规定是否需要在安全的 HTTPS 连接来传输 cookie。如果 cookie 需要在安全的 HTTPS 连接下传输,则设置为 TRUE。默认是 FALSE。


Tips and Notes

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

Note: When sending cookie, the value of the cookie will automatically be URL encoded.Automatically decoding URL received. If you do so, you can use setrawcookie () instead.


Example 1

Set up and send 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 methods to retrieve cookie values ​​(after the cookie settings):

<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

By the expiration date to a past date / time, remove a cookie:

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

<html>
<body>

...
...


Example 4

Create an array of 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 Reference Manual Complete PHP HTTP Reference Manual