How does PHP operate cookies?

青灯夜游
Release: 2023-04-07 21:00:01
forward
2417 people have browsed it

How does PHP operate cookies?

Cookies are text files stored on the client's computer and they are retained for tracking purposes. PHP transparently supports HTTP cookies.

Identifying returning users involves three steps

● The server script sends a set of cookies to the browser. Such as age, etc.

●The browser stores this information locally on your computer for future use

●The next time the browser sends any request to the web server, it will send these cookie information to the server, The server uses this information to identify the user.

This chapter will teach you how to set cookies, how to access them and how to delete them.

The Anatomy of a Cookie

Cookies are typically set in HTTP headers (although JavaScript can also set cookies directly on the browser). A PHP script that sets a cookie may send a header that looks like this (seen by browser F12, network)

HTTP/1.1 200 OK Date: Fri, 04 Feb 2000 21:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT; path=/; domain=jc2182.com Connection: close Content-Type: text/html
Copy after login

As you can see, the Set-Cookie header contains name-value pairs, GMT date, path and domain. Names and values will be URL encoded. The expires field is the browser's instruction to delete the cookie after a given time and date. If the browser is configured to store cookies, this information will be retained until the expiry date. If the user points the browser to any page that matches the cookie's path and domain, it will resend the cookie to the server. The browser's header might look like this

GET / HTTP/1.0 Connection: Keep-Alive User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc) Host: zink.demon.co.uk:1126 Accept: image/gif, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8 Cookie: name=xyz
Copy after login

The PHP script can then access the environment variable $_COOKIE, which contains all cookie names and values.

Set Cookies with PHP

PHP provides the setcookie() function to set cookies. This function takes up to six parameters and should be called before thetag. This function must be called individually for each cookie set.

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

Here are the details of all parameters

●name - Set the name of the cookie.

● Value - Sets the value of the named variable to what you actually want to store.

● Expire - This specifies the time in the future (in seconds) since 00:00:00 GMT on January 1, 1970. After this time, the cookie will be inaccessible. If this parameter is not set, the cookie will automatically expire when the web browser is closed.

● Path - specifies the directory where the cookie is valid. A single forward slash character allows the cookie to be valid for all directories.

●domain - This can be used to specify domain names in very large domains and must contain at least two validity periods. All cookies are only valid for the host and domain where they were created.

● Security - Can be set to 1 to specify that the cookie should only be sent over secure transport using HTTPS, otherwise set to 0, which means the cookie can be sent over regular HTTP.

The following example will create two cookies Name and Age, these cookies will expire after one hour.

   用PHP设置Cookies 
Copy after login

Tip: The time() function returns the current timestamp, which is the number of seconds from 0:00:00 on January 1, 1970 to the moment when the script is executed.

Open the browser to access the script, then press F12 to open the developer mode, select the Network tab, select Headers, you can see the following picture:

How does PHP operate cookies?

Accessing Cookies using PHP

PHP provides many ways to access cookies. The easiest way is to use the $_COOKIE variable. The following example will access all cookies set in the above example.

  用PHP访问cookie 
"; echo $_COOKIE["age"] . "
"; ?>
Copy after login

You can use the isset() function to check whether the cookie is set. If set, then output.

  用PHP访问cookie 
"; if(isset($_COOKIE["age"])) echo $_COOKIE["age"] . "
"; ?>
Copy after login

Deleting Cookies with PHP

Formally speaking, to delete a cookie, you should just call setcookie() with the name parameter [that is, you want to delete that name , setting it to null], but this does not always work and should not be relied upon. The safest way is to set an expired date

/ Set the past time to 60 seconds before the current time/

   用PHP删除cookie 
Copy after login

Recommended learning:PHP tutorial

The above is the detailed content of How does PHP operate cookies?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
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!