Forum Moderators: coopster

Message Too Old, No Replies

php header location question

php header location question

         

drooh

9:18 pm on Feb 11, 2010 (gmt 0)

10+ Year Member



Ok, I know that you need to use
header('location: /'); before any html output or whitespace but I have just noticed that it only throws the error "Cannot modify header information – headers already sent" only when I have a certain amount of content before the php header('location: /') command. this seems really weird to me.

For instance

if I have
<html>
just a few lines of info
header('location: /')

Then there is no error

But if I have
<html>
about 50 lines of info or more
header('location: /')

then I get the error
"Cannot modify header information – headers already sent"

So It doesn't really matter to me because I always use that command at the top of my code before any html output but for testing purposes I would like to be alerted if I forget or accidentally do it wrong.

Has anyone else had this happen?

Matthew1980

9:42 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Drooh,

I am never sure whether this is just coding practice personal preference, or even whether it matters or not, but, in the header(); function - the use of "" quotes as opposed to the ''.

The amount of people who I see using the single quote version surprises me, I was always taught double quotes, and I try to advocate this method of practise to anyone who asks.

With regards to the:-


"Cannot modify header information – headers already sent"


I make sure that there is no <html> or whitespace before I place a header("Location: FULL_URL/index.php"); as this is where the error thrown comes from, but I have not noticed an occurrence like the one as you have raised.

I don't suppose that there is a <title> or <body> tag within the 50 lines of code is there? as these tags have information in them which are echoed to the title bar, or the main viewing pane of the browser, which would result in the error being flagged up.

Cheers,

MRb

Trav

9:55 pm on Feb 11, 2010 (gmt 0)

10+ Year Member



It's hard to say without seeing the intervening code, but I'd say that there's something within the 50+ lines that's producing an output- an echo statement or something. You could try encapsulating the intervening code in an output buffer (search 'ob_start' on php.net) and then grabbing the data out of the buffer after you send your headers.

drooh

10:54 pm on Feb 11, 2010 (gmt 0)

10+ Year Member



there was HTML tags etc above, but i tried slowly removing different pieces and the best I can tell is that there was a certain "file size" limit that triggered the error.

and replacing the single quotes with double did not make a difference.

im thinking it has something to do with a buffer setting in the php.ini

Readie

3:10 am on Feb 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



*Edited*
Could a mod delete this post please? :)

rocknbil

8:09 pm on Feb 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know why your first example doesn't kick the same error. It should. From the documentation [us2.php.net]:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.


What this is telling you, from your original example,

<html>
just a few lines of info

The previous is output. So issuing a header after "<html>just a few lines" is going to kick this error, because it has automatically issued a content-type:text/html header merely by the presence of "some content" before the header.

Note that this is not really a PHP issue, PHP just confuses the issue. When you request a document, you can send any number of headers, several of them, if you want. But those all must come first before document output. Once the document begins output, you can't send another header. That's just the way it is. :-) If you did this in any other language,

print "content-type:text/html\n\n"; # content-type header
print '<p>Here is some output</p>';
print "content-type:text/html";

You would get the header as content.

<p>Here is some output</p>
content-type:text/html

The example from the link above is pretty explicit,


<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>


If you need to do something before issuing header(), wrap it all in PHP and don't output anything until it's ready.


include("some-file-with-functions.php");
$errors = cleanse_data();
if ($errors) { header("location:/try-again.php"); }
<!-- the previous will EXIT -->
<html>
<head><title>Blah</title></head>
.....