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
膝盖疼吃什么药好 | 白羊座是什么性格 | 学信网上的报告编号是什么 | 什么是双相情感障碍 | 午时五行属什么 |
有个性是什么意思 | array是什么意思 | 脾胃虚弱吃什么水果 | 过敏性咽炎吃什么药 | 肠息肉是什么原因造成的 |
癃闭是什么意思 | 飞机打多了会有什么后果 | 胎儿左侧侧脑室增宽的原因是什么 | 氯化钠是什么 | 孽障是什么意思 |
彩霞是什么意思 | 老是说梦话是什么原因 | 元肉是什么 | 腰部疼痛挂什么科 | 脾阳不足吃什么中成药 |
蛔虫是什么意思hcv8jop1ns8r.cn | 有机会是什么意思imcecn.com | 刻舟求剑的求是什么意思hcv8jop7ns1r.cn | bcr是什么意思hcv8jop2ns7r.cn | 健身吃蛋白粉有什么好处和坏处hcv8jop5ns0r.cn |
三个六代表什么意思hcv8jop9ns9r.cn | 怀孕生化了是什么原因hcv9jop3ns3r.cn | 又什么又什么的花hcv9jop0ns7r.cn | 痰多吃什么药好hcv9jop1ns5r.cn | 抬旗是什么意思ff14chat.com |
情花是什么花hcv9jop6ns3r.cn | 考拉吃什么食物hcv9jop4ns0r.cn | 巩固是什么意思jingluanji.com | 一直放臭屁是什么原因hcv8jop2ns3r.cn | 审阅是什么意思hcv9jop2ns9r.cn |
晕厥是什么意思hcv9jop7ns4r.cn | 高中学考是什么意思hcv9jop4ns6r.cn | 牛的本命佛是什么佛bfb118.com | 三个降号是什么调hcv8jop6ns5r.cn | 女性绝经有什么征兆hcv8jop1ns8r.cn |