-1

My header location fails to work on a server wich uses php 5.3.21. It does work on a localhost with 5.4.7. It is backwards compatible.. can anybody tell my why this is wrong?

switch ($table) {
    case "hardcover":
    header('location:hardcover.php?type=Aanvraag&relID='.$relID.'');
    break;
} 

It is part of a switch case that decides where to go after you have entered a certain value from a HTML option list. When i echo $relID, and $table in the same case, it does echo the items. But somehow it wont redirect to the header..

Result from: ini_set("display_errors", "On");

Warning: Cannot modify header information - headers already sent by (output started at /customers/f/f/e/tdmdev.nl/httpd.www/graficrm/htmlheader.php:7) in /customers/f/f/e/tdmdev.nl/httpd.www/graficrm/aanvradd.php on line 47

Line 47 is the line where the header is located. Line 7 is where a session is echoed into html title area.

afarazit
  • 4,907
  • 2
  • 27
  • 51
SliQz
  • 298
  • 1
  • 4
  • 17
  • I found in PHP4 onwards that headers should be followed by an exit(); to stop the script continuing. (Which did happen) – Waygood Feb 14 '13 at 08:50
  • 1
    @Waygood - If you want the script to terminate, you need that exit()... I've seen too many security flaws where people use header() to redirect to an error page, but don't exit afterwards, so the entire script still runs before the redirect can take effect – Mark Baker Feb 14 '13 at 08:52
  • Add this line at the top of your page: `ini_set("display_errors", "On");` then post the output so that we could close the question as a duplicate. – Salman A Feb 14 '13 at 08:54
  • [Check this out](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php/8028987#8028987). And i think the last part of the header must be : `'.$relID."'");` – HamZa Feb 14 '13 at 09:00
  • @HamZa DzCyberDeV Tried it, still no result header("Location:hardcover.php?type=Aanvraag&relID='".$relID."'"); – SliQz Feb 14 '13 at 09:09
  • @SliQz you're sending data before `header()` – HamZa Feb 14 '13 at 09:10
  • 1
    @HamZa DzCyberDeV Thank you. The link to "Possible Duplicate" gave me the solution. – SliQz Feb 14 '13 at 09:19
  • You're saying `Line 7 is where a session is echoed into html title area.` So you're already outputting data to the browser. You can't do that if you want to use `header()`. That is causing the problem. – w00 Feb 14 '13 at 16:29

2 Answers2

1

Try ob_start() at the beginning of the page.

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
Kandarp Patel
  • 1,045
  • 9
  • 21
0
switch ($table) {
    case "hardcover":
    header('Location:hardcover.php?type=Aanvraag&relID='.$relID.'');
    exit();   // <-stop and allow header to do its thang!
    break;
} 
Waygood
  • 2,657
  • 2
  • 15
  • 16