Redirect to page and send custom HTTP headers
Solution 1
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 header
Docs 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
Solution 2
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.
Related videos on Youtube
Lennie
Updated on July 09, 2022Comments
-
Lennie almost 2 years
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?
-
hakre over 12 yearsMultiple
header
calls. -
Lennie over 12 yearsI tried adding it before and after the redirect, I then use HttpFox on Firefox but see that the HTTP headers aren't being sent.
-
T9b over 12 yearsAre you trying to send data with your redirect? Why don't you use php sessions?
-
-
hakre over 12 yearsWhat didn't work for you? Please post your code. I can't guess out of the blue what you're doing wrong.
-
Lennie over 12 yearsheader("VIA: maga"); header("X_MSISDN: +27000000000"); header("X_UP_CALLING_LINE_ID: +27000000000"); header("Location: url");
-
hakre over 12 yearsYou need to disable automatic redirects in firefox to actually see which response headers are send. If you don't, Firefox will do a second request to the URI given in the
Location
header which is a different thing then. You can disable the automatic redirect temporarly by commenting out theLocation
header or you can disable it by installing a little Add-On: NoRedirect 1.2.4 Toolpress Strict Edition (Firefox Add-On) -
sepehr over 7 yearsIt doesn't work because you can't set custom HTTP headers for a redirect. Never!
-
hakre over 7 yearsSounds a bit as if op got puzzled between request and response headers.
-
wpcoder over 6 years
header("Location: http://...");
will redirect before the second line run. -
Luis Muñiz almost 5 yearsThis is not the correct answer, if your response is interpreted by an agent. If you examine the response with a very low-level http client, you may be able to extract the added headers.
-
Ankit Kumar over 4 yearsit is not possible
-
hakre almost 4 years@BretRoyster: Please see this old comment here: stackoverflow.com/questions/7583461/… --- response headers can be send with any HTTP response, and if the HTTP client creates another request, it's a new thing.
-
Bret Royster almost 4 years@hakre - deleted my last comment
-
Alexander over 3 years@genesis gone? where?? is everything OK?
-
hakre almost 3 years@sepehr: Not for a redirect, and I never wrote that it's possible for a redirect. But it is perfectly technically possible to send other (custom) response headers and the
Location
response header (e.g.X-Example-....
). The location header represents the redirect to another page, and the other headers are custom headers. This qualifies the and in question title. If that may make no sense is a different judgement apart from properties of the protocol in use which has been asked for. Don't shoot the messenger. -
sepehr almost 3 years@hakre: I have no idea what I talked about back then and how these things work anymore 😅 But I trust you have a point there.