cookie usage

Create Cookie

Create a cookie in PHP through the setcookie() function. The syntax format is as follows:

bool setcookie(string name[,string value[,int expire[, string path[,string domain[,int secure]]]]])

Read Cookie

In PHP, you can read the cookie value on the browser side directly through the super global array $_COOKIE[].

    PHP语言基础 
"; } else { setcookie("visittime", date("y-m-d H:i:s"), time() + 60); echo "上次访问时间为:" . $_COOKIE["visittime"]; echo "
"; } echo "本次访问时间:" . date("y-m-d H:i:s"); ?>


First run result:
This is the first time to save Cookie
Time of this visit: 16-07- 16 08:26:25

Second running result:
Last visit time: 16-07-16 08:26:25
This time visit time: 16-07-16 08:27:25

The above code first detects whether the Cookie file exists through the isset() function. If it does not exist, create a Cookie file through the setcookie() function. If it exists, set the cookie expiration time to 60 seconds.

Delete Cookie

When a cookie is created, if its expiration time is not set, the cookie file will be automatically deleted when the browser is closed. If you want to delete the Cookie file before closing the browser, there are two methods: one is to delete it using the setcookie() function, and the other is to manually delete the Cookie in the browser. They are introduced separately below.

1. Use the setcookie() function to delete cookies

Deleting cookies is basically similar to creating cookies. The setcookie() function is also used to delete cookies. To delete cookies, you only need to set the second parameter in the setcookie() function to a null value, and set the expiration time of the third parameter Cookie to be less than the current time of the system.

For example, to set the cookie expiration time to the current time minus 1 second, the code is as follows:

setcookie("name", "", time()-1);

In the above code, the time() function returns the current timestamp expressed in seconds. Subtracting 1 second from the expiration time will get the past time, thereby deleting the cookie.

2. Manually delete cookies in the browser

When using cookies, the cookie automatically generates a text file and stores it in the Cookies temporary folder of the IE browser. Deleting cookie files in your browser is a very convenient method.

Cookie life cycle

If the cookie does not set a time, it means that its life cycle is during the browser session. As long as the IE browser is closed, the cookie will automatically disappear. This kind of cookie is called a session cookie and is generally not saved on the hard disk but in memory.

If the expiration time is set, the browser will save the cookie to the hard disk, and it will still be valid when you open the IE browser again until its validity period expires.

Although cookies can be stored in the client browser for a long time, they are not static. Because the browser is allowed to store up to 300 cookie files, and each cookie file supports a maximum capacity of 4KB; each domain name supports a maximum of 20 cookies. If the limit is reached, the browser will automatically delete cookies at random.

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

If your application involves browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. One way is to pass data from a form (we covered forms and user input earlier in this tutorial).

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

  
Name: Age:

Retrieve the value in "welcome.php" like this:

  Welcome .
You are years old.


Continuing Learning
||
PHP语言基础
"; } else { setcookie("visittime", date("y-m-d H:i:s"), time() + 60); echo "上次访问时间为:" . $_COOKIE["visittime"]; echo "
"; } echo "本次访问时间:" . date("y-m-d H:i:s"); ?>
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!