Home  >  Article  >  Backend Development  >  How to clear browser cookies in php server

How to clear browser cookies in php server

coldplay.xixi
coldplay.xixiOriginal
2020-11-03 15:49:552297browse

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 in php server

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!

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