-1

I have just uploaded some new PHP code onto my live server and I am now getting the classic "Cannot modify header information - headers already sent..." error. It worked perfectly fine on my local WAMP server.

The server error log shows this:

[30-Jan-2014 03:29:28 America/New_York] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/donald/public_html/admin/process/itemMgmt.php:12) in /home/donald/public_html/includes/functions.php on line 158

Line 8 through 15 in itemMgmt.php is this:

require("../wideimage/lib/WideImage.php");
$url = $_POST['url'];

$name = str_replace(' ','_',$_POST['name']);
echo "GIVEN NAME: " .$name. "<br>";//Error Checking

$loc = $_POST['loc'];
echo "LOC: " .$loc. "<br>";//Error Checking

And line 154 through 161 in functions.php is this:

function redirect_to( $location = NULL )
{
if ($location != NULL)
    {
    header("location: ".$location."",true,302);
    exit;
    }
}

This process is used when I add a new item into my database and is used across multiple pages on my site. I get this same error regardless of which page I try to add a new item from.

I have searched this site and found several write ups on this exact subject, however I cannot find one where the poster had it working on a local server but not on a live one, which I believe makes this a unique problem.

I have searched to make sure there are no white-spaces before, or after, my <?php ?> tags and found everything ok.

I'm most confused by the line numbers given in the error log, it doesn't make any sense and I can't figure out where to look next.

Andrew Fox
  • 794
  • 4
  • 13
  • 30
  • are you used `ob_start()` in top of your code? – saravankg Jan 31 '14 at 09:53
  • 2
    try remove echo lines.. they cause headers to be sent – user1587985 Jan 31 '14 at 09:53
  • No, it's not a unique problem at all. It has been answered in-depth [here](http://stackoverflow.com/a/8028987/476), including *"But it worked on my other server!"* – deceze Jan 31 '14 at 09:54
  • @deceze Thank you for pointing that out. I was reading through that link you posted, however I missed the part "But it worked on my other server!" – Andrew Fox Jan 31 '14 at 10:00
  • @user1587985 that did it, thank you very much. Please add it as an answer and I will mark it correct. – Andrew Fox Jan 31 '14 at 10:03
  • 2
    A downvote? Really? It's like people take it personally if you do honest research and then post a question because you can't find an answer only to find out you overlooked a tiny section of a huge write up. – Andrew Fox Jan 31 '14 at 10:11

2 Answers2

0

Add @ob_start(); function in top of the page, Where you have called redirect_to() function.

Krish R
  • 22,583
  • 7
  • 50
  • 59
0

header() function must be called before any html is outputed.so header() must be at top of the page, also separating the logical part (php functions and checks) from view (html/css/js) is a good practice.

Arrok
  • 116
  • 5