juwens
juwens

Reputation: 3897

howto remove specific http Response headers in PHP

your are setting some specific headers with the same name multiple times. For example

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

Afterwards you like to delete only "Set-Cookie: Bar", but not the other ones. How to do this?

header_remove doens't work here becaue you can specify the name "Set-Cookie", but not specific the Cookie "Set-Cookie: Foo".

Upvotes: 1

Views: 703

Answers (1)

juwens
juwens

Reputation: 3897

It's not nice, but it works:

The solution is to copy all headers, delete all, filter and create again.

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

Upvotes: 2

Related Questions