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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
parkej60
Forum Newbie
Posts: 2
Joined: Fri May 07, 2010 10:27 am

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

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post by John Cartwright »

Sounds like a hidden character.. which is relatively common. Try saving the file through notepad to see if that helps.
parkej60
Forum Newbie
Posts: 2
Joined: Fri May 07, 2010 10:27 am

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

Post 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:
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post by mikosiko »

try deleting everything but your php line
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
pcjeffmac
Forum Newbie
Posts: 4
Joined: Fri May 07, 2010 3:12 pm

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

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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.
pcjeffmac
Forum Newbie
Posts: 4
Joined: Fri May 07, 2010 3:12 pm

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

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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.
bigwatercar
Forum Newbie
Posts: 2
Joined: Wed Nov 12, 2008 12:59 am

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

Post by bigwatercar »

Code: Select all

<?php
ob_start();
//your code here
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post by John Cartwright »

bigwatercar wrote:

Code: Select all

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