douchang8758 2012-01-30 11:19
浏览 38
已采纳

我们可以根据客户的时间在php中设置cookie吗?

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?

  • 写回答

1条回答 默认 最新

  • dsolwotv00116 2012-01-30 11:58
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改