Home  >  Article  >  Backend Development  >  How to make php cookie invalid after closing the browser

How to make php cookie invalid after closing the browser

藏色散人
藏色散人Original
2020-10-16 09:44:593440browse

php How to set the cookie to become invalid after closing the browser: first open the corresponding code file; then use the "setcookie("stat",time());" statement to achieve the invalidation after closing the browser.

How to make php cookie invalid after closing the browser

Recommended: "PHP Video Tutorial"

"I have been writing a program recently and have been thinking about how to use PHP The cookie will expire when the browser is closed or after 24 hours?"

For PHP, it is very simple to set the cookie to expire when the browser is closed, as long as the date is not written, for example:

setcookie("mark",1);

Many people think that if you write it like this, it will definitely become invalid after 24 hours. Who wouldn’t sleep for 24 hours? Yes, it is normal to sleep within 24 hours, but there are many people who do not turn off their phones within 24 hours.

How to make it invalid after 24 hours? After thinking about it, I can only consider it in terms of time.

Suddenly thought of the following writing method:

setcookie("stat",date("d"));  //date()函数里面的$timestamp 是可以不写的,不写的时候,默认本地当前时间

This seems to be OK, but for night owls in the middle of the night, it is troublesome. I accessed the program at 23:59 and restarted one minute later. Recorded, this is unreasonable.

In the end we can only consider using the time interval to calculate

setcookie("stat",time());

You need to judge whether the time interval reaches 24 hours when you re-record next time.

The above is the detailed content of How to make php cookie invalid after closing the browser. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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