PHP cookie cleanup and ff cookies cannot be deleted _ php Tutorial-PHP Tutorial

Source: Internet
Author: User
PHP cookies cannot be deleted. We use setcookie to set and delete cookies in php. it is automatically deleted when it expires. However, you may encounter a cookie that cannot expire in ff. First, let's take a look at the php manual setting and deleting cookies in php. We use setcookie to set the settings and will automatically delete the settings when they expire, but in ff, you may encounter a cookie 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 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
Example

The code is as follows:

// 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:

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: // 127.0.0.1/php/rss2fla/data/log. 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:

Simple:

The code is as follows:

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

With expiration time:

The code is as follows:

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

Everything:

The code is as follows:

SetCookie ("FullCookie", "Full cookie value", time () + 3600, "/forum", ". bKjia. c0m", 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:
If (isset ($ _ COOKIE ["sid"]) &! Empty ($ _ COOKIE ["sid"]) {
// This ensures that the Cookie exists and is valid
......
}

Bytes. First, let's take a look at the php Manual...

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.