Wondering how to fix PHP header not redirecting issue? We can help you.
This redirection error occurs due to some incorrect output buffering settings and wrong header formats.
At Bobcares, we often receive requests to fix redirection errors in PHP as part of our Server Management Services.
Today, let’s discuss this error in detail and see how our s Support Engineers fix it effectively.
Steps to fix PHP header not redirecting
Before going into the steps for fiing this issue let’s see some uses of PHP header?
The header() function in PHP sends a raw HTTP header. This raw data contains the request made by the server as information.
And, we call header() before any actual output is sent. It gives a clear idea about the object sent in the message body.
Also, the Location: parameter of the header returns a redirect status code to the browser.
Usually, we can redirect it either to a URL or some file.
In PHP, redirection to a URL will be of the following format:
< ?php header("Location: http://www.redirect.to.url.com/"); ?>
Similarly, to redirect to a file, we use the following command:
<?php header("Location: anotherDirectory/anotherFile.php"); ?>
Often this redirection results in some tricky errors, we will see how our Support Engineers fix them.
Fixing PHP header not redirecting
PHP redirections are usually set when we get failed sessions. We can check this with the code given below:
if(
!isset($_SESSION['SESS_MEMBER_ID'])
|| (trim($_SESSION['SESS_MEMBER_ID']) == '')
AND !isset($fb_login)
) {
header("location:login.php?msg=Please+Login+Again.");
}
For failed session logins, we will try redirection to another page and this is when we notice that redirection has failed and a black page showed up.
This generally happens due to an error with output buffering.
If we use header(“Location: “);, then it is necessary to use ob_start(); earlier in the script.
ob_start() function will turn the output buffering ON. While output buffering is active no output is sent from the script other than headers until we call ob_end();
Calling header(“Location: “); after content has already been output to the browser can cause problems.
So, we ensure that no output is sent before calling the header.
Setting ob_start() at the beginning of the PHP file heps to resolve the error.
Similarly, errors in the header format can also cause redirection failures.
It is important to make the “Location” start with uppercase. Also, the header contents must be inside double-quotes.
[Still facing PHP redirection errors?- We’ll help you.]
Conclusion
In short, the PHP header not redirecting error occurs mainly due to the absence of ob_start() function, incorrect header formats, and so on. Today we discussed this error in detail and saw how our Support Engineers fix this for our customers.
Fixwd My Probllem
Thank you for this helpful article, which led me to the solution after hours of searching for the error. Although the program worked fine on my local xampp-server, I had a white-screen (blank-screen) on the internetserver.
Using ob_start() and ob_end() helped.
Hi Bernd,
Thanks for the feedback.We are glad to know that our article helps you solves the issue.
Fixed my problem by this solution . Thankyou!
Hi,
Thanks for the feedback.We are glad to know that our article helps you solves the issue.
Thank you very much !!
ob_start() was the solution.
Best regards
Hi,
Thanks for the feedback. We are glad to know that our article was helpful for you 🙂 .
thanks for your post. it works for me.
Hi keiner,
Thanks for the feedback. We are glad to know that our article was helpful for you 🙂 .