Are you trying to resolve PHP header not redirecting issue?
This guide is for you
PHP header not redirecting error occurs due to some incorrect output buffering settings and wrong header formats.
The header() function is an predefined PHP native function. With header() HTTP functions we can control data sent to the client or browser by the Web server before some other output has been sent.
In this context, we shall look into how to fix Website redirection issues.
Methods to fix PHP header not redirecting ?
Before going into the steps for fixing 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 to resolve them.
How to fix PHP header not redirecting ?
PHP redirections are usually set when we get failed sessions.
We can check this with the code given below:
|| (trim($_SESSION['SESS_MEMBER_ID']) == '')
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 helps 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.