PHP Cookie


Cookies are commonly used to identify users.


What is a Cookie?

Cookies are commonly used to identify users. A cookie is a small file that the server leaves on the user's computer. Whenever the same computer requests a page through a browser, this computer will send the cookie. Through PHP, you can create and retrieve the value of cookies.


How to Create a Cookie?

The setcookie() function is used to set a cookie.

Note:The setcookie() function must appear before the <html> tag.

Syntax

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

Example 1

In the example below, we will create a cookie named "user" and assign it the value "example". We also specify that this cookie expires after one hour:

<?php
setcookie("user", "example", time()+3600);
?>

<html>
.....

Note:When sending a cookie, the cookie value is automatically URL-encoded, and automatically decoded when retrieved. (To prevent URL encoding, use setrawcookie() instead.)

Example 2

You can also set the cookie expiration time in another way. This may be simpler than using seconds.

<?php
$expire=time()+60*60*24*30;
setcookie("user", "example", $expire);
?>

<html>
.....

In the example above, the expiration time is set to one month (60 seconds * 60 minutes * 24 hours * 30 days)。


How to Retrieve the Cookie Value?

PHP's $_COOKIE variable is used to retrieve the cookie value.

In the example below, we retrieve the value of the cookie named "user" and display it on the page:

<?php
// 输出 cookie 值
echo $_COOKIE["user"];

// 查看所有 cookie
print_r($_COOKIE);
?>

In the example below, we use the isset() function to confirm whether a cookie has been set:

<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
</head>
<body>

<?php
if (isset($_COOKIE["user"]))
    echo "欢迎 " . $_COOKIE["user"] . "!<br>";
else
    echo "普通访客!<br>";
?>

</body>
</html>


How to Delete a Cookie?

When deleting a cookie, you should change the expiration date to a point in the past.

Example of deletion:

<?php
// 设置 cookie 过期时间为过去 1 小时
setcookie("user", "", time()-3600);
?>


What if the Browser Does Not Support Cookies?

If your application needs to deal with browsers that do not support cookies, you will have to use other methods to pass information between pages in your application. One way is to pass data through forms (we have already covered forms and user input in earlier chapters of this tutorial).

The form below submits user input to "welcome.php" when the user clicks the "Submit" button:

<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
</head>
<body>

<form action="welcome.php" method="post">
名字: <input type="text" name="name">
年龄: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html>

Retrieve the values in the "welcome.php" file, as shown below:

<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
</head>
<body>

欢迎 <?php echo $_POST["name"]; ?>.<br>
你 <?php echo $_POST["age"]; ?> 岁了。

</body>
</html>
Other Extensions