Custom Headers in PHP Redirects: An Impossible Request
When redirecting to a page using PHP, you may encounter a challenge in attempting to pass along custom HTTP headers with the redirect. The standard method for redirection involves using the header("Location: http://...") syntax. However, this approach only sets headers for the response that triggers the redirect, not for the redirect itself.
In reality, it is not possible to redirect to a page with custom headers set. Regardless of the language or framework utilized, there is no mechanism to initiate an HTTP redirect while simultaneously instructing the client (browser) to add a custom header.
While it may seem logical to use multiple header() calls, this approach will not succeed. The custom headers you set will only affect the response that initiates the redirect, not the redirect itself.
The sole method for a website to instruct a browser to issue an HTTP request with a custom header is through JavaScript and the XMLHttpRequest object. However, this approach requires CORS implementation on the target server to permit such asynchronous XMLHttpRequests.
It is crucial to remember that a page cannot set HTTP request headers unless it makes an asynchronous request using XMLHttpRequest. This implies that you cannot implement redirection with a custom header on the client side either.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3