This is a very strange issue. In my code I have a redirect that works perfectly on my local server.
header("location:/sign-up-success");
When I push to production, it just doesn't redirect. Is there a setting that I am missing?
I have even tried:
header("Location: https://www.myurl.com/sign-up-success");
It appears to just skip over the redirect. Any ideas?
Solution to the Problem To solve this problem, we have to store the header in a buffer and send the buffer at the end of the script, so to store the header in the buffer, we will use the php ob_start() function and to clean the buffer, we will use the ob_end_flush() function. See the below code snippet. This is it!
Fixing PHP header not redirecting 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.
To create a PHP redirect, you first need to write your header() function. This begins with header(). Next, define the Location response-header field with the URL or file name where you want to redirect users and search engines. Place that within the parentheses.
possible reason: you have sent some output to the browser before the call of header()
solution : write ob_start()
at the top of the page
Best practice : alwyas write exit()
after header()
..
Reference
Take a look at value of error_reporting
on production server. If it is set to too low level, there will be nothing in the log, as errors of lower level than error_reporting
are just silently ignored. Same applies to using @
- it sets error_reporting
to 0, so if anything bad happens (e.g., if function is not even defined), you won't see anything in the log.
From what you wrote about enabling output buffering, it seems that you have some output before header()
(this is why enabling output buffering helps) and that your error_reporting
is set to 0 (this is why warning about "Cannot modify header information" was not being reported/logged).
On a side note... To get the most of error reporting:
error_reporting
to E_ALL | E_STRICT
(in both dev and production environments)display_error
to true
in dev environment, false
in production environment (critical!!! user does not have to see any PHP warnings/notices/errors)set_error_handler()
to output or log more information than default error handler does (e.g., you might want to store debug_backtrace()
, when error occurs)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With