Php cookie cleanup and ff cookies cannot be deleted

Source: Internet
Author: User
Tags setcookie

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 must ensure that its expiration time is in the past to trigger the deletion mechanism of the browser.
The following example shows how to delete the cookie you just set: Example 2. setcookie () Delete

The code is as follows: Copy code

// Set the expiration time to one hour ago

Setcookie ("TestCookie", "", time ()-3600 );
Setcookie ("TestCookie", "", time ()-3600 ,"/~ Rasmus/",". utoronto. ca ", 1 );

To store user login information, and then use

The code is as follows: Copy code

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

Exit. There is no problem in testing in IE. As a website, you must be compatible with as many browsers as possible. As a result, it was normal to log on to Firefox and encountered problems when it was launched. The user is always logged on. So I checked the differences between cookie Records in IE and Firefox. After testing, I suddenly realized.

If the fourth parameter (valid path parameter) of setcookie () is not specified, the current directory is used as the valid path by default, and the path I tested is: http: // php. Therefore, the cookie paths set during login and exit are different.

IE is more user-friendly than Firefox. Haha, when a path is specified, it will overwrite the Cookie variable of the same name under the current IP address, while FireFox is more strict, resulting in a new variable ......

Here are several examples:


The code is as follows: Copy code

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

With expiration time:

The code is as follows: Copy code

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


The code is as follows: Copy code

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

The last one is compatible with all browsers.

In summary, it is best to use the following method when operating cookies in php:

The code is as follows: Copy code
If (isset ($ _ COOKIE ["sid"]) &! Empty ($ _ COOKIE ["sid"]) {
// This ensures that the Cookie exists and is valid
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.