Home  >  Article  >  Backend Development  >  Understanding and using PHP Cookie related knowledge

Understanding and using PHP Cookie related knowledge

jacklove
jackloveOriginal
2018-05-07 12:00:251365browse

PHP Cookie plays an important role in PHP caching. This article provides some detailed explanations of its related knowledge.

What are Cookies?

Cookies are often 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?

The setcookie() function is used to set cookies.

Note: The setcookie() function must be located before the 100db36a723c770d327fc0aef2ce13b1 tag.

Syntax

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

Example 1

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

.....

Note: The cookie value is automatically URL-encoded when sending the cookie 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.

.....

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:

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

菜鸟教程(runoob.com)
";else echo "普通访客!
";?>

How todelete Cookie?

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

Deleted example:

8c580e3a110b04970f479d507da46b72

What should I do if my browser does not support Cookies?

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 (forms and user input are covered in the previous chapters of this tutorial).

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

菜鸟教程(runoob.com)
名字: 年龄:

Retrieve the value in the "welcome.php" file, as follows Shown:

菜鸟教程(runoob.com)
欢迎 .
岁了。

This article provides a detailed understanding of Cookie-related knowledge. For more learning materials, please pay attention to the php Chinese website to view.

Related recommendations:

PHP Session’s understanding and application of cache-related knowledge

About PHP array sorting Knowledge application

#Related knowledge and application of PHP file upload

The above is the detailed content of Understanding and using PHP Cookie related knowledge. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn