Home  >  Article  >  Backend Development  >  How to use cookies to set expiration time in php?

How to use cookies to set expiration time in php?

coldplay.xixi
coldplay.xixiOriginal
2020-07-10 09:46:382753browse

How to use cookies to set the expiration time in php: Set the cookie to expire after one day, the code is [setcookie ("mycookie", "123", time() 3600*24);], [time() 3600*24] represents the timestamp of the next 24 hours.

How to use cookies to set expiration time in php?

How to use cookies to set the expiration time in php:

1. Introduction to the setcookie function

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

Parameters:

  • name Set the name of the cookie. The value of this parameter must be set.

  • #value Set the cookie value. This parameter must be set.

  • expire Optional. Set the validity period of the cookie, usually a timestamp.

  • #path Optional. Set the server path for cookies.

  • domain Optional. Domain name to set cookie.

  • secure Optional. Specifies whether cookies are transmitted over a secure HTTPS connection.

It can be seen that to set the cookie expiration time, you only need to set three parameters. Please see the following example:

2. Set the cookie expiration time

Set the 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);

Related learning recommendations:PHP programming from entry to proficiency

The above is the detailed content of How to use cookies to set 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