How to get cookie's expire time

前端 未结 6 571
小蘑菇
小蘑菇 2020-12-01 02:37

When I create a cookie, how to get cookie\'s expire time?

6条回答
  •  旧巷少年郎
    2020-12-01 02:58

    It seems there's a list of all cookies sent to browser in array returned by php's headers_list() which among other data returns "Set-Cookie" elements as follows:

    Set-Cookie: cooke_name=cookie_value; expires=expiration_time; Max-Age=age; path=path; domain=domain

    This way you can also get deleted ones since their value is deleted:

    Set-Cookie: cooke_name=deleted; expires=expiration_time; Max-Age=age; path=path; domain=domain

    From there on it's easy to retrieve expiration time or age for particular cookie. Keep in mind though that this array is probably available only AFTER actual call to setcookie() has been made so it's valid for script that has already finished it's job. I haven't tested this in some other way(s) since this worked just fine for me.

    This is rather old topic and I'm not sure if this is valid for all php builds but I thought it might be helpfull.

    For more info see:

    https://www.php.net/manual/en/function.headers-list.php
    https://www.php.net/manual/en/function.headers-sent.php

提交回复
热议问题