Understanding and using PHP Cookie related knowledge

jacklove
Release: 2023-03-25 13:50:01
Original
1414 people have browsed it

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 thetag.

Syntax

setcookie(name, value, expire, path, domain);
Copy after login

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:

.....
Copy after login

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.

.....
Copy after login

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:

Copy after login

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

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

How todeleteCookie?

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

Deleted example:


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)
名字: 年龄:
Copy after login

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

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!