Setting of cookie attributes and validity period

Setting of cookie attributes and validity period

You can see the cookie information in the resources tab of the Chrome browser console.

Cookie attributes:

The name field is the name of a cookie.

The value field is the value of a cookie.

The domain field is the domain name that can access this cookie.

The path field is the path of the page where the cookie can be accessed. For example, if the domain is abc.com and the path is /test, then only pages under the /test path can read this cookie.

The expires/Max-Age field is the timeout period of this cookie. If the value is set to a time, then when this time is reached, the cookie becomes invalid. If not set, the default value is Session, which means that the cookie will be invalidated with the session. When the browser is closed (not the browser tab, but the entire browser), this cookie becomes invalid.

The size of this cookie in the Size field.

The httponly attribute of the http field cookie. If this attribute is true, only the cookie information will be included in the http request header, and the cookie cannot be accessed through document.cookie.

The secure field sets whether this cookie can only be passed through https

Setting of validity period:

1. dateadd法

1) Set the expiration date to 0:0:00 on the Nth day after the current date

Response.Cookies(“LastView”).Expires=dateadd(“d”,N,date)
2) Set the time limit to 0:0:0 on the same day of the Nth month after the current date

Response.Cookies(“LastView”).Expires=dateadd(“m”,N,date)

3) Set the time limit to the hour, minute, and second on the Nth day after the current time

Response.Cookies(“LastView”).Expires=dateadd(“d”,N,now)

4) Set the time limit to the hour, minute, and second on the N+M day after the current time

Response.Cookies(“LastView”).Expires=dateadd(“d”,N,now+M)

5) Set the time limit to the hour, minute, and second of the Nth month after the current time

Response.Cookies(“LastView”).Expires=dateadd(“m”,N,now)

6) Set the time limit to the hour, minute, and second on the Mth day after the same day of the Nth month after the current time

Response.Cookies(“LastView”).Expires=dateadd(“m”,N,now+M)

2. Fixed date method

1) Set the time limit to 0: 0: 0 seconds on a certain day to expire

Example: to expire at 0:00:00:00 on January 29, 2003
Response.Cookies(“LastView”).Expires=# 1/29/2003 #
or
Response.Cookies(“LastView”).Expires=# 2003/1/29 #
or
Response.Cookies(“LastView”).Expires=“January 29,2003”

2) Set the deadline to expire at a certain time on a certain day

Example: It will expire at 21:0:0 on January 29, 2003
Response.Cookies(“LastView”).Expires = #2003/1/29 21:00:00#
or
Response.Cookies(“LastView”).Expires = #1/29/2003 21:00:00#
or
Response.Cookies(“LastView”).Expires = #January 29,2003 21:00:00#

In fact, in this method, using "" "" and "# #" have the same effect. For example
Response.Cookies(“LastView”).Expires=“January 29,2003”
and
Response.Cookies(“LastView”).Expires=# January 29,2003 # 的效果就是相同的。

3. date+ \now+ 法

The main structure of this method is Date+mathematical formula or now +mathematical formula. Set the expiration time of the Cookie to a certain period of time after the current time through a simple method of four arithmetic operations. The plus sign here means to add the preset time on the basis of the current time.

1) date+ method

If the time limit is set to 0:00, 0:00 on the Nth day after the current date, we can use the method mentioned above or the following format:
Response.Cookies(“LastView”).Expires=Date+N

The date+ method is a deadly method. It is not very different from the above methods, and can be replaced by the above methods. The following introduces the now+ method, which is a more flexible method, which can accurately locate the failure time to any year, one month, one day, one hour, one minute, and one second after the current time.

2)now+法

Let me give you an example: Response.Cookies(“LastView”).Expires = now+1

The function of this sentence is to limit the expiration time of the Cookie to the same time 1 day later. The 1 here stands for 1 day, that is, 24 hours. Since now represents the system time of the current Web server, including year, month, day, hour, minute, and second, the expiration date of the Cookie expressed by this sentence is on the same hour, minute, and second tomorrow. If it is changed to now+2, it means that the expiration date of this cookie is at the same hour, minute, and second two days later; now+30 means that the expiration date of this cookie is at the same hour, minute, and second one month later; now+30 *2 means that the expiration date of this cookie is on the same hour, minute and second two months later; now+365 means that the expiration date of this cookie is on the same hour, minute, and second one year later...

When 1 is divided into 24 equal parts, that is, the sentence becomes: Response.Cookies(“LastView”).Expires = now+1/24it means that the expiration time of the Cookie is limited to one hour later.
On this basis, divide 1 into 60 equal parts, that is, change now+1/24 to now+1/1440, which means that the expiration time of the Cookie is limited to one minute later. The 1440 here is derived from 24*60.

Similarly, to limit the expiration time of Cookie to ten minutes later, change the program toResponse.Cookies(“LastView”).Expires = now+10/1440;

To limit the expiration time of Cookie to one second later, the program becomes Response.Cookies(“LastView”).Expires = now+1/86400.
Of course, it is theoretically possible to limit the expiration time of Cookie to microseconds. However, there is no practical significance.

In addition, if the program statement becomes: Response.Cookies(“LastView”).Expires = now+1+1/1440it means that the expiration time of the cookie is 24 hours and one minute later.

Guess you like

Origin blog.csdn.net/Guesshat/article/details/109570923