简体   繁体   English

多次设置cookie header

[英]set-cookie header multiple times

I have this code in a assets controller to get images:我在资产 controller 中有这段代码来获取图像:

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);
}

I have set csrf protection to true in config/config.php file and every request for an image is sent with Set-Cookie header. So the csrf-cookie can get set multiple times on some pages.我在config/config.php file中将 csrf 保护设置为true ,并且每个图像请求都使用Set-Cookie header 发送。因此 csrf-cookie 可以在某些页面上设置多次。 Is that something to worry about, and if so, is there a way to prevent this?这是需要担心的事情吗?如果是,是否有办法防止这种情况发生?

I managed to do this with header_remove("set-cookie");我设法用header_remove("set-cookie");做到了这一点

So the code looks like this所以代码看起来像这样

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);

If in only one page/image request you uses setcookie function many times, php will send many times the same cookie to browser in one response.如果仅在一个页面/图像请求中多次使用setcookie function,php 将在一次响应中多次向浏览器发送相同的 cookie。 Maybe some browsers crashes reading that.也许某些浏览器在阅读时崩溃了。

I've had problems with ajax requests in Inte.net Explorer due to multiple cookie definitions, when accidentally start the session object in CakePHP into a loop.由于多个 cookie 定义,我在 Inte.net Explorer 中遇到了 ajax 请求的问题,当不小心将 CakePHP 中的 session object 启动到循环中时。 I only detected that problem sniffing the connection with wireshark.我只在嗅探与 wireshark 的连接时检测到该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM