Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create, read and delete Cookie in PHP

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to complete the creation, reading and deletion of Cookie in PHP". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to create, read and delete Cookie in PHP.

In our daily life, logging on to a website will contain the information you left when your login was interrupted last time, and the goods you saw on different pages will also be seen on the shopping cart, which is a means used by the website to identify users. in order to easily push content to users. To achieve such a complex function, you need to use cookie, so let's take a look at what Cookie is. What is its purpose and how to create, read, and delete.

What is Cookie?

Many functions can be achieved through cookie, so what is cookie? Cookie is a way to save a small amount of data passed by the server to the browser in the user's browser under the HTTP protocol. For example, it can save some user information, so that users can still maintain the data even if the browser is closed or the connection is broken.

It can also be understood that cookie is to save the specific information of the browser web page in the form of a file on the client hard disk. When you visit the website again, the browser will first read the cookie file and provide the data to the browser. In this way, a similar user's web page has some personalized settings, which will be saved if you open it again. This is to show them through cookie.

Main uses of Cookie

There are many uses of cookie, and there are many kinds of information in cookie. The server can use the arbitrariness of cookie to filter and maintain information in order to determine the state of HTTP transmission. The most typical application of cookie is also very common in our daily life, which is to determine whether a registered user has logged on to the site, and similar functions in shopping carts in the mall.

Let's sum up that cookie is commonly used in the following three aspects:

Record some information about the user; similar to the user personalization mentioned above.

Pass data between pages; that is, when the data of one page is to be used by another page.

Saving the viewed Web page in a temporary cookie file can speed up the access of the page.

We need to note that it is generally not recommended to use cookie to save datasets or larger data, and not all browsers support cookie, and the data information is saved in the form of text, in order not to affect the security of the website, it is best not to save sensitive unencrypted data.

Create cookie

To create a cookie in PHP, you need the setcookie () function. In creating a cookie, we need to note that cookie is part of the HTTP response header, which must be output first. If you output a HTML or blank line or echo before the setcookie () function, it may cause an error in the program.

The syntax format of the setcookie () function is as follows:

Setcookie (string $name [, string $value = "" [, int $expire = 0 [, string $path = "" [, string $domain = "" [, bool $secure = false [, bool $httponly = false])

One of the things we need to pay attention to is:

$name--- indicates the name of the setting Cookie

$value--- is an optional parameter that can be used to set the value of Cookie

Expire--- is an optional parameter that sets the expiration time of Cookie, which is in the form of a Unix timestamp. Some cookie are temporary and some are persistent, temporary ones will exist on the browser for a certain period of time, and once the specified time is exceeded, the cookie will be cleared by the system.

Optional parameter $path---, which is used to set the server path that is valid for Cookie.

Optional parameter $domain---, which is used to set the valid domain name / subdomain name of Cookie.

The $secure--- optional parameter is used to set whether the Cookie is passed to the client only through a secure HTTPS connection.

Next, take a look at creating a cookie through an example:

Output result:

What we need to note at run time is that there is no output when the script file is run for the first time, so we need to refresh the page after setting Cookie, so that the HTTP header will carry the last set Cookie information on the next request, and then the Cookie can be read.

Read cookie

In PHP, to read cookie, you need to use the super global variable $_ COOKIE,$_COOKIE, which is an array that stores all the information. Its syntax format is as follows:

$_ COOKIE ['Cookie name']

Examples are as follows:

Output result:

So we read the value of cookie through $_ COOKIE.

Delete cookie

We said above, you can set a time for the temporary cookie, when the time has passed, the system will automatically delete the cookie, whether it can be created after Cookie is created, without setting its expiration time, its Cookie file will be automatically deleted when the browser is closed. If you want to delete the Cookie file before closing the browser, you also need to use the setcookie () function.

Deleting a Cookie is similar to creating a Cookie, only by using the setcookie () function to set the value of Cookie (that is, the second parameter) to null, or to set the expiration time of Cookie (that is, the third parameter) to less than the current time of the system.

Examples are as follows:

Output result:

So we are done deleting the value of cookie.

Thank you for your reading, the above is the content of "how to complete the creation, reading and deletion of Cookie in PHP". After the study of this article, I believe you have a deeper understanding of how to complete the creation, reading and deletion of Cookie in PHP. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report