如何在PHP中删除特定的http响应头

时间:2013-03-21 16:52:54

标签: php

您正在多次设置一些具有相同名称的特定标头。例如

header("Set-Cookie: Foo=bar");
header("Set-Cookie: Bar=foo");
header("Set-Cookie: Baz=bob");

之后你想只删除“Set-Cookie:Bar”,而不是其他的。 怎么做?

header_remove这里不起作用,因为你可以指定名称“Set-Cookie”,但不是特定的Cookie“Set-Cookie:Foo”。

1 个答案:

答案 0 :(得分:2)

这不好,但它有效:

解决方案是复制所有标题,删除所有标题,过滤并再次创建。

$headers = headers_list();
header_remove(); // removes all headers
foreach($headers as $h) {
    if (!preg_match("/^Set-Cookie: Bar$/", $h)) {
        header($h);
    }
}