Home  >  Article  >  Backend Development  >  How to set cookie expiration time in php

How to set cookie expiration time in php

王林
王林Original
2020-08-29 15:12:223074browse

How to set cookie expiration time in php: You can use the setcookie() function to set it. This function is used to send an HTTP cookie to the client. The specific usage is as follows: ["mycookie", "123", time() 3600*24)].

How to set cookie expiration time in php

# The setcookie() function sends an HTTP cookie to the client. If successful, the function returns TRUE. Returns FALSE on failure.

(Recommended tutorial: php video tutorial)

Function syntax:

setcookie(name,value,expire,path,domain,secure)

Parameter introduction:

  • 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 "/", the cookie will be valid within the entire domain name. If the path is set to "/test/", 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 on 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. The default is FALSE

(Related recommendations: php training)

Example:

Set cookies after one day Expiration and invalidation

setcookie ("mycookie", "123", time()+3600*24);

time() represents the timestamp of the current time, and time() 3600*24 represents the timestamp of the next 24 hours.

The above is the detailed content of How to set cookie expiration time in php. For more information, please follow other related articles on the PHP Chinese website!

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