Let’s give a small example to explain what cookie
When people had meetings decades ago, they all needed to bring one Participation certificate. This participation card contains the person's position, name, unit, photo and other information. During the meeting, the security personnel and organizers of the conference only need to check the relevant information
##.
This small example mainly explains that people take their own attendance certificates and their own information. This pattern is the cookie.
# counter is commonly used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. With PHP, you can create and retrieve cookie values.
How to create a cookie?
setcookie()
The function is used to set cookies. Note: The setcookie() function must be located before the <html> tag.
grammar
setcookie
(name,value,expire,path,domain)name Required. Specifies the name of the cookie. value Optional. Specifies the value of the cookie.
expire Optional. Specifies the validity period of the cookie.
path Optional. Specifies the server path for cookies.
Domain optional. Specifies the domain name for the cookie.
For this function, we generally use it like this: SetCookie (cookie name, cookie value, cookie validity period), three are enough
Example
In the following example, we will create the name For the cookie "user", assign it the value "Alex Porter". We also stipulate that this cookie expires after an hour: <?php
setcookie("user","Alex Porter",time()+3600);
?>
## Note : When sending cookies URL encoding, automatically decoded on retrieval (to prevent URL encoding, use setrawcookie() instead).
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", "php", $expire); ?>In the above example, the expiration time is set to one month (60 seconds * 60 minutes * 24 hours * 30 days).
How to retrieve the value of Cookie?
PHP’s $_COOKIE variable is used to retrieve the value of the cookie.
In the following example, we retrieve the value of the cookie named "user" and display it on the page:
<?php // 输出 cookie 值 echo $_COOKIE["user"] ."<br/>"; // 查看所有 cookie print_r($_COOKIE); ?>
We created a cookie named php above cookie, so the program running result is as follows:
php
Array ( [pgv_pvi] => 9622684672 [user] => php )
Example
##In the following example, We use the isset() function to confirm whether the cookie has been set:
<html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <?php header("Content-type:text/html;charset=utf-8"); if (isset($_COOKIE["user"])) echo "欢迎 " . $_COOKIE["user"] . "!<br>"; else echo "普通访客!<br>"; ?> </body> </html>
Program execution result:
Welcome to php!
How to delete cookies?
Example
The cookie named "user" is assigned the value "XX user". It is specified that this cookie will expire after one hour:<?php header("Content-type:text/html;charset=utf-8"); //设置编码 setcookie("user","XX用户",time()+3600); ?>If you want to delete the above cookie, just set the time as follows
<?php setcookie("user","",time()-3600); ?>
Example
The following form submits the user to "cookie_welcome.php" when the user clicks the "Submit" button Enter: <html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<form action="cookie_welcome.php" method="post">
名字: <input type="text" name="name">
年龄: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
Retrieve the value in the "cookie_welcome.php" file, as shown below:
<?php header("Content-type:text/html;charset=utf-8"); echo "欢迎".$_POST['name'] ."<br>"; echo "你".$_POST['age'] ."岁了"; ?>
Program running result:
Welcome liuqiYou are 23 years old
#Next Section