【问题标题】:set-cookie header multiple times多次设置cookie标头
【发布时间】:2012-01-12 00:08:38
【问题描述】:

我在资产控制器中有这段代码来获取图像:

function images($path,$image_name)
{
    $image = "../assets/images/$path/$image_name";

    if (file_exists ($image) && (is_file($image))) {
        $name = $image_name;
    } else {

    }

    $file = getimagesize($image);
    $filesize = filesize($image);

    $time_cache = 360000000000;
    $ts = gmdate("D, d M Y H:i:s", time() + $time_cache) . " GMT";
    header("Content-Type: {$file['mime']}\n");
    header("Content-disposition: inline; filename=\"$name\"\n");
    header("Content-Length: $filesize\n");
    header("Expires: $ts");
    header("Pragma: cache");
    header("Cache-Control: max-age=$time_cache");
    readfile ($image);
}

我在config/config.php file 中将csrf 保护设置为true,并且每个图像请求都带有Set-Cookie 标头。所以 csrf-cookie 可以在某些页面上设置多次。有什么需要担心的吗?如果有,有没有办法防止这种情况发生?

【问题讨论】:

    标签: php codeigniter cookies


    【解决方案1】:

    如果只在一个页面/图像请求中多次使用setcookie 函数,php 将在一个响应中多次向浏览器发送相同的cookie。也许有些浏览器会崩溃。

    由于多个 cookie 定义,当不小心将 CakePHP 中的会话对象启动到循环中时,我在 Internet Explorer 中遇到了 ajax 请求问题。我只检测到嗅探与wireshark的连接的问题。

    【讨论】:

    • 我通过header_remove("set-cookie");成功做到了这一点,你知道这样做有什么副作用吗?
    • 您将删除所有 cookie,即使是由其他组件(或会话管理器)声明的那些,所以您需要重新声明所有标题。如果您想使用 headers_list 之类的东西来执行此操作,请解析并重新声明最后也许可行...我认为在全局范围内使用标志变量更好(也更简单)...奇怪但可以快速运行...
    • 不太清楚你的意思,它对我有用,会话和 csrf 工作,它不会为每个资产设置新的 cookie。
    【解决方案2】:

    我设法通过header_remove("set-cookie"); 做到了这一点

    所以代码看起来像这样

    header("Content-Type: {$file['mime']}\n");
    header("Content-disposition: inline; filename=\"$name\"\n");
    header("Content-Length: $filesize\n");
    header("Expires: $ts");
    header("Pragma: cache");
    header("Cache-Control: max-age=$time_cache");
    header_remove("set-cookie");
    readfile ($image);
    

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2017-07-08
      • 2020-09-28
      • 2013-07-24
      相关资源
      最近更新 更多