You can use the setcookie() function in PHP to set the expiration time of cookies. The syntax is "setcookie(name,value,expire,path,domain,secure)"; the expire parameter is used to specify the validity period of the cookie, that is, the expiration timestamp.
# The setcookie() function sends an HTTP cookie to the client.
Cookie is a variable sent by the server to the browser. Cookies are typically small text files that a server embeds on a user's computer. This cookie is sent each time the same computer requests a page through the browser.
[Related tutorial recommendation: "PHP Tutorial"]
The name of the cookie is automatically assigned to a variable with the same name. For example, if the cookie being sent is named "user", a variable named $user will automatically be created containing the cookie's value.
The cookie must be assigned before any other output is sent to the client.
If successful, this function returns TRUE. Returns FALSE on failure.
Syntax
setcookie(name,value,expire,path,domain,secure)
Parameters:
Parameters | Description |
---|---|
name | Required. Specifies the name of the cookie. |
value | Required. Specifies the cookie value. |
expire | Optional. Specifies the cookie expiration time. time() 3600*24*30 will set the cookie expiration time to 30 days. If this parameter is not set, the cookie will automatically expire after the session ends (that is, when the browser is closed). |
path | Optional. Specifies the server path for cookies. If the path is set to "/", then the cookie will be valid within the entire domain name. If the path is set to "/test/", then the cookie will be valid under the test directory and all its subdirectories. The default path value is the current directory where the cookie is located. |
domain | Optional. Specifies the domain name for the cookie. In order for the cookie to be valid in all subdomains of example.com, you need to set the domain name of the cookie to ".example.com". When you set the domain name of the cookie to www.example.com, the cookie is only valid in the www subdomain. |
secure | Optional. Specifies whether cookies need to be transmitted over a secure HTTPS connection. Set to TRUE if the cookie needs to be transmitted over a secure HTTPS connection. Default is FALSE. |
Set cookie expiration time
Set cookie to expire after one day
setcookie ("mycookie", "123", time()+3600*24); /* http://www.manongjc.com/article/1258.html */
time() represents the timestamp of the current time, time() 3600*24 represents the timestamp of the next 24 hours.
Similarly, set the cookie to expire after one year
setcookie ("mycookie", "123", time()+3600*24*365);
Delete the cookie
By setting the expiration date to a date/time in the past, Delete a cookie:
setcookie("username", "", time()-3600,"/amazon"); setcookie("lastlogin", "", time()-3600,"/amazon"); setcookie("userid", "", time()-3600,"/amazon");
Delete all cookies for the current session
foreach($_COOKIE as $key=>$val){ setcookie($key,"",time()-3600,"/amazon");
The above is the detailed content of How to set expiration time for php cookies?. For more information, please follow other related articles on the PHP Chinese website!