Skip to main content

Can we set a cookie in php according to client"s time?



I have following requirements:





  1. create a cookie for server domain



  2. that cookie will expire in x seconds say in 200 or 500 seconds.







Problem is, that clients can lag as much as many minutes behind server. On server side I am setting cookie as







setcookie($cooName,$cooVal,time()+500,"/");







but now if client computer is 500 seconds behind server, above code will effect into a cookie which will expire in 1000 seconds not 500 seconds.





I was thinking to send client's time stamp to server and set cookie on that time. something like this:







setcookie($cooName,$cooVal,$_GET['clientTS']+500,"/");







But if client is 500 seconds behind, and if I set such a cookie which is backdated it does not get set. How to achieve a time sync between client and server in case of cookie expiry?


Comments

  1. Unfortunately, Expires is an absolute date and depends on the user agent’s local date. As you have concluded correctly, this could lead to an inaccurate cookie expiry.

    This is also the reason why IETF’s first standardization of Netscape’s original proposal, replaced the absolute expiration date by a relative expiration date, the Max-Age attribute that specified the time in delta seconds from the point in time the cookie has been issued. RFC 2965, that obsoleted RFC 2109, did the same. Just as RFC 6265, that is currently the most recent specification for cookies.

    Cookies as per RFC 6265 do also allow to specify the expiry date by both a relative date using Max-Age and a absolute date using Expires, the latter primarily for backwards compatibility:


    If a cookie has both the Max-Age and the Expires attribute, the Max-Age attribute has precedence and controls the expiration date of the cookie.


    So you could write your own function that mimics this behavior:

    $maxage = 12345;
    $expires = date(DATE_COOKIE, time()+$maxage);
    header("Set-Cookie: $name=$value, Expires=$expires, Max-Age=$maxage, …");




    Here’s an example function:

    function set_cookie($name, $value=null, $maxage=null, $path=null, $domain=null, $secure=false, $httponly=false) {
    $cookie = rawurlencode($name) . '=' . rawurlencode($value);
    $attributes = array();
    if (!is_null($maxage)) {
    $maxage = intval($maxage);
    $attributes[] = 'Expires='.date(DATE_COOKIE, $maxage > 0 ? time()+$maxage : 0);
    $attributes[] = 'Max-Age='.$maxage;
    }
    if (!is_null($path)) {
    $attributes[] = 'Path='.rawurlencode($path);
    }
    if (!is_null($domain)) {
    $attributes[] = 'Domain='.rawurlencode($domain);
    }
    if ($secure) {
    $attributes[] = 'Secure';
    }
    if ($httponly) {
    $attributes[] = 'HttpOnly';
    }
    header('Set-Cookie: '.implode('; ', array_merge(array($cookie), $attributes)), false);
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex