Home >Backend Development >PHP Problem >How to clear browser cookies in php server
How the PHP server clears browser cookies: 1. Set the expiration time of the cookie; 2. Set the cookie value to empty, the code is [setcookie($cookiename, '') setcookie($cookiename, NULL) ;].
How to clear browser cookies by php server:
1. Setting cookies Expiration time
//将过期时间设为一小时前 setcookie("TestCookie", "", time() - 3600); setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);
2. Set the cookie value to be empty
setcookie($cookiename, '') setcookie($cookiename, NULL);
3. The problem of setcookie taking effect immediately in php
Use the method setcookie to set the cookie, but it will not work after setcookie It will take effect immediately. If you get $_cookie immediately, you will not get the value. Refresh the page again to get the value.
Look at the cookie item in the network in the chrome developer tools, you will see that there is no value in the request cookie, but there is a value in the response cookie (I don’t know the difference between them yet)
If you want it to take effect immediately, you can setcookie(cookiename,value)
and then$_cookie[cookiename]=value
The first step is to create a cookie, and the second step is Assignment.
Related learning recommendations: php programming (video)
The above is the detailed content of How to clear browser cookies in php server. For more information, please follow other related articles on the PHP Chinese website!