header() function will not work - Warning: cannot.....
Moderator: General Moderators
header() function will not work - Warning: cannot.....
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.
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.
- 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.....
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.....
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.
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.
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>
[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.
Re: header() function will not work - Warning: cannot.....
try deleting everything but your php line
Re: header() function will not work - Warning: cannot.....
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.....
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.
<?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.
- 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.....
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 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.
Re: header() function will not work - Warning: cannot.....
"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.
"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.
- 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.....
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.pcjeffmac wrote:or you get creative with the output control functions available to you in PHP
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.....
Code: Select all
<?php
ob_start();
//your code here
?>- 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.....
Boy oh boy. To each his own, I guess.bigwatercar wrote:Code: Select all
<?php ob_start(); //your code here ?>