Home  >  Article  >  Backend Development  >  How does PHP operate cookies?

How does PHP operate cookies?

青灯夜游
青灯夜游forward
2019-11-29 14:21:232468browse

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

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

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 the tag. This function must be called individually for each cookie set.

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

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
   setcookie("name", "John Watkin", time()+3600, "/","", 0);
   setcookie("age", "36", time()+3600, "/", "",  0);
?>
<html>
   
   <head>
      <title>用PHP设置Cookies</title>
   </head>
   
   <body>
      <?php echo "设置 Cookies"?>
   </body>
   
</html>

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.

<html>
   
   <head>
      <title>用PHP访问cookie</title>
   </head>
   
   <body>
      
      <?php
         echo $_COOKIE["name"]. "<br />";
         
         echo $_COOKIE["age"] . "<br />";
  
      ?>
      
   </body>
</html>

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

<html>
   
   <head>
      <title>用PHP访问cookie</title>
   </head>
   
   <body>
      
      <?php
         if(isset($_COOKIE["name"]))
                echo $_COOKIE["name"]. "<br />";
         if(isset($_COOKIE["age"]))
                echo $_COOKIE["age"] . "<br />";
  
      ?>
      
   </body>
</html>

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
   /* 设置过去时间为当前时间的之前的60秒 */
   setcookie( "name", "", time()- 60, "/","", 0);
   setcookie( "age", "", time()- 60, "/","", 0);
?>
<html>
   
   <head>
      <title>用PHP删除cookie</title>
   </head>
   
   <body>
      <?php echo "删除cookie" ?>
   </body>
   
</html>

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!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete