Search code examples
cookiesphp4

set-cookie expiration in seconds


Does the html header Set-Cookie function accept expiration in seconds?

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

$expireSeconds = time()+$expireSeconds;

NOTE: I dont want to use set cookie because i am running php4 version. Also php4 does not support httponly in the setcookie() function


Solution

  • The proper date format for expires is something like this:

    Mon, 19 Nov 2012 15:40:59 GMT
    

    That format can be obtained with this snippet:

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

    Or:

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

    Expiry date of 30 days in the future can be done with:

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

    The max-age can be used to specify (in seconds) when the cookie should expire; this is not portable between all browsers though, as explained here.