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 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: |
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: // 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: |
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 |
Everything:
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.
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 ...... } |