PHP Cookie
Let’s give a small example to explain whatcookie
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 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 enoughExample
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:
## 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. 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:
"; // 查看所有 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:
php中文网(php.cn) "; else echo "普通访客!
"; ?>
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:If the browser does not support Cookie, this what to do? If your application needs to deal with browsers that do not support cookies, then you will have to use other methods to pass information between pages in your application. One way is to pass data through a form (we have already covered forms and user input in previous chapters of this tutorial).
Example
The following form submits the user to "cookie_welcome.php" when the user clicks the "Submit" button Enter:
Retrieve the value in the "cookie_welcome.php" file, as shown below:php中文网(php.cn)
"; echo "你".$_POST['age'] ."岁了"; ?>
Program running result:
Welcome liuqiYou are 23 years old
#