How to set expiration time for php cookies?

青灯夜游
Release: 2023-03-02 18:48:01
Original
4244 people have browsed it

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.

How to set expiration time for php cookies?

# 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)
Copy after login

Parameters:

Parameters Description
nameRequired. Specifies the name of the cookie.
valueRequired. Specifies the cookie value.
expireOptional. 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).

pathOptional. 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.

domainOptional. 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.

secureOptional. 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 */
Copy after login

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);
Copy after login

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");
Copy after login

Delete all cookies for the current session

foreach($_COOKIE as $key=>$val){
setcookie($key,"",time()-3600,"/amazon");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!