Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headers not re-directing (and there aren't any errors)

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?

like image 238
Jason Avatar asked May 03 '11 00:05

Jason


People also ask

Why header() is not working in PHP?

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!

Why is header not redirecting in PHP?

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.

How to redirect header in PHP?

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.


2 Answers

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

like image 120
diEcho Avatar answered Sep 24 '22 06:09

diEcho


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:

  • set error_reporting to E_ALL | E_STRICT (in both dev and production environments)
  • enable error logging (critical for production environment, though it won't hurt to have it enabled in dev environment as well)
  • set display_error to true in dev environment, false in production environment (critical!!! user does not have to see any PHP warnings/notices/errors)
  • additionally, you might want to 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)
like image 30
binaryLV Avatar answered Sep 22 '22 06:09

binaryLV