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

How to make php cookie invalid after closing the browser

藏色散人
Release: 2023-03-06 17:38:02
Original
3516 people have browsed it

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);
Copy after login

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 是可以不写的,不写的时候,默认本地当前时间
Copy after login

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());
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template