Home  >  Article  >  Backend Development  >  PHP cookie clearing and cookies cannot be deleted under ff_PHP tutorial

PHP cookie clearing and cookies cannot be deleted under ff_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:391094browse

We use setcookie to set and delete cookies in php, and they will be automatically deleted when they expire. However, under ff, you may encounter cookies that cannot expire.

First, let’s take a look at the instructions on deleting cookies in the PHP manual

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

To delete a cookie, you need to ensure that its expiration date is in the past to trigger the browser's deletion mechanism.
The following example illustrates how to delete the cookie just set: Example 2. setcookie() delete
Example

The code is as follows Copy code
 代码如下 复制代码

// 将过期时间设为一小时前

setcookie("TestCookie", "", time() - 3600);
setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);

//Set the expiration time to one hour ago

setcookie("TestCookie", "", time() - 3600);
 代码如下 复制代码

setcookie("username", "", time()-3600);

setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);

Wait to save the user's login information, and then use
The code is as follows Copy code
setcookie("username", "", time()-3600);

Exit and test under IE without any problems. Since you are building a website, you must be compatible with as many browsers as possible, haha. So I tested it in Firefox and everything worked fine when logging in. But when I launched it, I ran into trouble. There is no way to log out, the user is always logged in. So I checked the difference between cookie records in IE and Firefox, and after testing, I suddenly realized it.

It turns out that if the fourth parameter (legal path parameter) of setcookie() is not specified, the current directory will be used as the legal path by default, and the path I tested is: http://127.0.0.1/php/rss2fla/data /log.php, so the cookie paths set when logging in and logging out are different.

IE is more user-friendly than Firefox. Haha, when specifying the path, it will overwrite the cookie variable with the same name under the current IP. However, FireFox is more strict, resulting in a new variable...

 代码如下 复制代码

SetCookie("MyCookie", "Value of MyCookie"); 

Let’s look at a few examples:

 代码如下 复制代码

SetCookie("WithExpire", "Expire in 1 hour", time()+3600);//3600秒=1小时 

Simple:

The code is as follows Copy code
SetCookie("MyCookie", "Value of MyCookie");
 代码如下 复制代码

SetCookie("FullCookie", "Full cookie value", time()+3600, "/forum", ".bKjia.c0m", 1); 

With expiration time:
The code is as follows Copy code
SetCookie("WithExpire", "Expire in 1 hour", time()+3600);//3600 seconds=1 hour
Everything is available:
The code is as follows Copy code
SetCookie("FullCookie", "Full cookie value", time()+3600, "/forum", ".bKjia.c0m", 1);

The last one is compatible with all browsers.

To sum up, it is best to use the following method when operating cookies in php:

 代码如下 复制代码
if(isset($_COOKIE["sid"]) && !empty($_COOKIE["sid"])){
  // 这样可以保证Cookie存在且在有效期内
  ……
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632780.htmlTechArticleTo set and delete cookies in php, we all use setcookie to set it. It will be automatically deleted when it expires, but in Under ff, you may encounter that the cookie cannot expire. First let’s take a look at the php manual...
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