Page 1 of 1

header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 10:35 am
by parkej60
Hello, I was just wondering if anyone could help me out with an issue I'm having. I'm just trying to get a page to run a header("Location:http://www.google.com") redirect and it will not work.

The only piece of code on the entire page is.

<?php header("Location:http://www.google.com"); ?>

There is no html on the page at all, but for some reason I get a "Warning: headers already sent error message".

If I run a <?php print_r(headers_list()); ?> I get:

Array ( [0] => X-Powered-By: PHP/5.2.10-2ubuntu6.4 [1] => Content-type: text/html )

I've got this to work on plenty of pages before, but for some reason on my development server it will not work. Is there a setting that needs to be changed? I'm running apache on Ubuntu.

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 12:41 pm
by John Cartwright
Sounds like a hidden character.. which is relatively common. Try saving the file through notepad to see if that helps.

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 12:54 pm
by parkej60
Checked for invisible characters in Text Wrangler and nothing but spaces show up where they should be.

I then recreated the file using Aptana and re-uploaded the file.

Here is code copied straight from the editor.

Code: Select all

<?php header('Location: http://www.google.com/'); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>Untitled Document</title>
	</head>
	<body>
		<p>Test</p>
	</body>
</html>
This is the exact error that shows up in my log.

[07-May-2010 12:57:05] PHP Warning: Cannot modify header information - headers already sent by (output started at /mnt/target03/364491/www.dcd-support.com/web/content/sandbox/index.php:1) in /mnt/target03/364491/www.dcd-support.com/web/content/sandbox/index.php on line 1

The only text that shows up on the page is the "Test" copy. :banghead:

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 1:17 pm
by mikosiko
try deleting everything but your php line

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 1:42 pm
by califdon
Is this page perhaps included in an earlier script? That error is saying that, before it got to this header directive, the web server has already sent SOMETHING to the browser. If there is even a BLANK LINE in the file before this, the server will have dutifully sent that blank line to the browser. It doesn't look like there is, in what you posted here, but that's the sort of thing that can generate that error.

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 3:19 pm
by pcjeffmac
Try putting this at the first line of the php script.

<?php ob_start(); ?>


Then put this at the end of the script.

<?php ob_end_flush(); ?>

This will tell php to wait until the script is complete before it writes the header.

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 3:32 pm
by John Cartwright
pcjeffmac wrote:Try putting this at the first line of the php script.

<?php ob_start(); ?>


Then put this at the end of the script.

<?php ob_end_flush(); ?>

This will tell php to wait until the script is complete before it writes the header.
Unfortunately, I cannot recommended this particular solution, and is merely a bandaid solution. Your best bet is to probably get an http traffic sniffer, such as fiddler2, to see exactly what is being sent to the browser.

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 4:04 pm
by pcjeffmac
"So where does that leave you? Well, either you make it a point to ensure that all sensitive code - like the code that generates HTTP headers - always appears right at the top of your scripts...or you get creative with the output control functions available to you in PHP."

"the advantage of using this technique should immediately become clear. By using a memory buffer to manage how the output of my script appears, I can do things which would otherwise be difficult or tedious - including sending HTTP headers to the browser *after* a script has started generating output, or (as you will see) displaying different Web pages on the basis of conditional tests within my script."

Just food for thought.

So, it would really depend on what you are trying to accomplish to determine if this is a bandaid.

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 4:29 pm
by John Cartwright
pcjeffmac wrote:or you get creative with the output control functions available to you in PHP
I wouldn't call a proper design having to be "creative". It is a bit silly to think output buffering is a solution just because the OP cannot identify where the output is being sent. It is complicating the issue where there should not be one to begin with.

That's not to say there are no positive uses for output buffering (quite the opposite), but I think it's fair to say this is not one of them.

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 8:23 pm
by bigwatercar

Code: Select all

<?php
ob_start();
//your code here
?>

Re: header() function will not work - Warning: cannot.....

Posted: Fri May 07, 2010 9:29 pm
by John Cartwright
bigwatercar wrote:

Code: Select all

<?php
ob_start();
//your code here
?>
Boy oh boy. To each his own, I guess.