15

I know this has been covered before but I cannot find an answer to this,

I have always used this;

header("Location: http://www.website.com/");
exit();

This has always worked in my current project and all of a sudden it is not working in any of my browsers

I would like to figure out the problem and fix it instead of using

echo "<script type='text/javascript'>window.top.location='http://website.com/';</script>";

I also have error reporting enabled and it shows no errors

// SET ERROR REPORTING
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
ini_set('display_errors', TRUE);

Any ideas why it will not work?

JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 4
    ^ is xor so: E_ALL ^ E_WARNING ^ E_NOTICE = 111111111 ^ 000001000 ^ 000000010 = 111110101 meaning all errors except warnings and notices. – danamlund Aug 06 '09 at 23:30
  • thanks I wish I would of found this out a long time ago, there must be thousands of notices now on my site =( – JasonDavis Aug 06 '09 at 23:37

13 Answers13

27

Try:

error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);


flush();
header("Location: http://www.website.com/");
die('should have redirected by now');

See what you get. You shouldn't use ^ (xor) in your error_reporting() call because you're unintentionally asking for all errors EXCEPT notices and warnings.. which is what a 'headers already sent' error is.

Edit:

Also try putting flush() right above your header() call.

Mike B
  • 31,886
  • 13
  • 87
  • 111
  • Yes that seems to be the problem, headers already sent, it says line 45 which is this; I don't see how to fix this yet – JasonDavis Aug 06 '09 at 23:46
  • Weird thing is I have been doing it this way for a long time on this server and it just now quit working, thing is I include a header file and the redirect is getting called in the included file so I am not sure how to make it work since the header file is already printed out by the time it reaches my redirect – JasonDavis Aug 06 '09 at 23:48
  • 4
    You can't have ANY output whatsoever before using header() – Mike B Aug 06 '09 at 23:49
  • As Mike B said. I removed my echo's before and now it works. :+1: – Marek Bernád Jul 21 '17 at 14:25
21

COMMON PROBLEMS:

1) there should not be any output (i.e. echo.. or HTML codes) before the header(.......); command.

2) there should not be a white-space(or newline) before <?php and after ?> tags.

3) GOLDER RULE! - the file (and other include()-d files) should have UTF8 without BOM encoding (and not just UTF-8). That is problem in many cases (because typical UTF8 encoded file has something special character in the start of file output)!!!!!!!!!!!

4) When redirecting, after header(...); you must use exit;

5) Recommended practice - always use 301 or 302 in reference:

header("location: http://example.com",  true,  301 );  exit;

6) If none of above helps, use JAVSCRIPT redirection(but it's highly not recommended By Google), but it may be the last chance...:

echo "<script type='text/javascript'>window.top.location='http://example.com/';</script>"; exit;
T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • **UTF-8** encoding! This solved it for me. Worked with encoding set to `UTF-8` in notepad++ but not with `UTF-8-BOM`, they [changed it](https://notepad-plus-plus.org/community/topic/7409/utf-8-without-bom-option) for notepad++ – bastelflp Dec 14 '15 at 20:09
13

Try removing the Space Between location and the first h in http.

header("Location: http://www.website.com/");
exit();

turns into

header("Location:http://www.website.com/");
exit();

I had this problem on my WAMP Server.

Although it shouldn't be the problem, considering that is how it is documented in the PHP documentation. But you should probably try it anyway. I know it has worked for me in a number of cases.

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
  • 1
    "removing the Space Between location and the first h in http." works like a charm, Many thanks. – Bhavin Rana Mar 12 '14 at 07:47
  • I had this problem as well. Safari, Explorer and Firefox seemed to work ok. Chrome was intermittent and mobile Safari just refused. Grr. – Mc.Stever Nov 27 '15 at 16:23
6

try this. worked for me.

echo "<meta http-equiv='refresh' content='0;url=http://www.yoursite.com'>";

karto
  • 3,538
  • 8
  • 43
  • 68
1

It may be strange solution but try this, change the page encoding from utf8 to ANSI and it will work.

use any text editor and save the page as ANSI encoding and upload it to your online server.

Mahmoud Salem
  • 104
  • 1
  • 5
1

Also when you are using the header function it has to be the first thing called before any text (even a space) is written to the client, so check again that there is no spaces being output prior to your call even before th

<?php
Toby Allen
  • 10,997
  • 11
  • 73
  • 124
1

Adding ob_start() solved this issue.

Gayathri
  • 126
  • 5
1

Try this (working for me):

echo '<script>window.location.replace("http://www.example.com")</script>';

Instead of

header("Location: http://www.example.com");

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Umair Sultan
  • 618
  • 6
  • 10
0

What exactly happens when you visit the page? You can try Firebug or any other tool that allows you to analyze HTTP headers and check if the redirect really happens and whether the Location header is really present.

Adam Byrtek
  • 12,011
  • 2
  • 32
  • 32
0

You should also verify that you are redirecting to a valid location, and that the location has proper 404 and 500 error messages/pages setup. It could be that you are simply redirecting a bad place.

arbales
  • 5,466
  • 4
  • 33
  • 40
0

Weird, but removing blank lines in php worked for me :-\

code before:

<?php

header("Location: http://www.website.com/");

?>

code that worked:

<?php header("Location: http://www.website.com/"); ?>
rlemon
  • 17,518
  • 14
  • 92
  • 123
lucorp
  • 36
  • 4
0

If your index page is a html file, it may not work. Change it to index.php and use this code:

<?php

header("Location: http://ea.tc");

?>
Emre AYDIN
  • 724
  • 8
  • 10
0

I actually had a case similar to this where I had an admin page that was included at the top of all my other pages. At the top of each page below the line:

<?php include '../../admin.php' ?>

I would have the php logic:

<?php if($_SESSION['username'] === null){ header("Location: ./adminLogin.php");}?>

The problem with this was that somewhere else I was also calling/manipulating the header(.... After a lot of time going through my code I admit I could not figure out where the problem was. Then I thought that each of these files hits my admin.php file before doing anything else. So I thought about what would happen if I would put the logic that was at the top of each of my views (because I didn't want anything to be visible unless you were logged in) into my admin.php file?

What happened was that before it even got to any of the php/html in my views it evaluated whether or not someone was logged in ($_SESSION['username'])) and if it was NULL then I just redirected to the adminLogin page. I put this logic right before my switch and it's worked perfectly for all my files that once required the logic. The way I had it worked in development, but posed a lot of issues in production. I found that moving the redirection logic to my admin.php file not only avoided the duplicate header(... manipulation but actually made my code more efficient by removing the excess logic from my view files and into my admin.php file.

Rather than putting the logic in every view file, put it in your controller once, before your switch. Works like a charm! This is useful if you don't want anyone to access any of the sensitive views unless they log in. In my case this was important for my CMS. However, if there are some files that you want to be viewable without logging in then I think the original logic would be sufficient. It seems like you already found a solution but hopefully this can be helpful if you run into this error again. :)

Matt Croak
  • 2,788
  • 2
  • 17
  • 35