set-cookie在几秒钟内到期

时间:2012-11-19 15:24:47

标签: cookies php4

html标头Set-Cookie功能是否在几秒钟内接受到期?

header( "Set-Cookie:". $cookieName."=".$sessId."; expires=".$expireSeconds."; sessionID=".$sessId.";path=".$path."; domain=".$domain."; httponly; secure);

$expireSeconds = time()+$expireSeconds;

注意: 我不想使用set cookie,因为我正在运行php4版本。另外php4在setcookie()函数中不支持httponly

2 个答案:

答案 0 :(得分:1)

如果您自己编写标题,则需要以此格式提供日期:

DAY, DD-MMM-YYYY HH:MM:SS GMT
DAY
    The day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
DD
    The day in the month (such as 01 for the first day of the month).
MMM
    The three-letter abbreviation for the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
YYYY
    The year.
HH
    The hour value in military time (22 would be 10:00 P.M., for example).
MM
    The minute value.
SS
    The second value.

但是,如果您使用的是PHP的setcookie()函数,则日期必须是Unix时间戳您可以使用mktime()。 time()+ 60 * 60 * 24 * 30会将cookie设置为30天后到期。如果设置为0或省略,则cookie将在会话结束时(浏览器关闭时)到期。

答案 1 :(得分:1)

expires的正确日期格式如下:

Mon, 19 Nov 2012 15:40:59 GMT

使用此代码段可以获得该格式:

str_replace('+0000', 'GMT', gmdate('r'));

或者:

gmdate('D, d M Y H:i:s T');

将来30天的失效日期可以通过以下方式完成:

$expires = str_replace('+0000', 'GMT', gmdate('r', strtotime('+30 days')));

max-age可用于指定cookie何时到期;(以秒为单位);但是,如here所述,这在所有浏览器之间都不可移植。