Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP header location-redirect doesn't work - why? [duplicate]

Tags:

Here's my file. I want to make it redirect, but nothing happens. To check out what is going on, I added an echo before the header part.

It neither throws an error or redirect to index.php. What is wrong? I have turned output buffering on/off, but nothing makes it redirect. What can I do?

<? error_reporting(E_ALL); echo 'This is an error';  header("Location: login.php"); die(); ?> 

Thanks

like image 257
Industrial Avatar asked Apr 25 '10 21:04

Industrial


People also ask

Why is my PHP redirect not working?

The root cause of this error is that php redirect header must be send before anything else. This means any space or characters sent to browser before the headers will result in this error. Like following example, there should not be any output of even a space before the headers are sent.

How automatically redirect to another page in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

What is PHP header location?

Basically, there are two types of header calls. One is header which starts with string “HTTP/” used to figure out the HTTP status code to send. Another one is the “Location” which is mandatory. replace: It is optional which indicates whether the header should add a second header or replace previous.


1 Answers

From PHP documentation :

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

And in your case, you are using echo before header()

like image 131
Soufiane Hassou Avatar answered Oct 11 '22 19:10

Soufiane Hassou