Lennie
Lennie

Reputation: 2069

Redirect to page and send custom HTTP headers

I use the following code to redirect to a page in PHP. I need to set a custom HTTP headers to pass along with the redirect.

header("Location: http://...");

How can I archive this?

Upvotes: 41

Views: 63055

Answers (3)

hakre
hakre

Reputation: 198204

This question needs to be answered differently as depending from where you're looking the answer is different:

Redirect Response (PHP)

As far as you're concerned about the redirect response (and not the request that may be triggered by a redirect response)

Multiple headerDocs calls:

header("Location: ....");
header("Header2: ....");
header("Header3: ....");
...

New Request triggered by a Redirect Response (Browser, HTTP client)

If you're looking for the new request that has been triggered by a redirect response, please consult your HTTP clients technical documentation for your options.

Commonly, HTTP clients do not offer any such options, that is most HTTP clients do not turn response headers into request headers in case one of the response headers is a Location: header and a status code in the 3xx range.

This would not make any sense anyway as such practice would be unsafe.

Especially on the level of interactive HTTP clients (like a browser) that automatically perform redirects without approval of the user. See HTTP, compare Fetch API.


Further reading: https://ec.haxx.se/http/http-redirects


An exclusion to the rules of the game outlined above is HTTP state management ¹ that most browsers support. E.g. you can respond with a cookie and the redirect request then sets the cookie. You perhaps might want to combine that with PHP session management. ²

¹ RFC 6265 - HTTP State Management Mechanism (ietf.org)

² PHP: Session Management Basics - Manual (php.net)

Upvotes: -20

sepehr
sepehr

Reputation: 18495

I'm afraid, all the answers are wrong and misleading!

It's impossible to redirect to a page with custom headers set, no matter what language or framework you use. In other words, there's no way to trigger an HTTP redirect and cause the client (browser) to add a custom header.

You might be thinking that using multiple header() calls should work just fine. But it won't. You're setting the custom headers for the response which is instructing the browser to redirect, not for the redirect itself.

The only way for a site to instruct a browser to issue an HTTP request with a custom header is to use Javascript and the XMLHttpRequest object. And it needs CORS implemented on the target server to allow such ajax requests.

Please note that a page can not set HTTP request headers unless it's making an async request using XMLHttpRequest. Meaning that you can't do such redirection with the custom header on the client-side as well.

Upvotes: 152

genesis
genesis

Reputation: 50982

Just add additional header() calls after or before this one.

header("Location: http://...");
header("Content-Type: text/plain");

Upvotes: -13

Related Questions