NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Header Rewrite Puzzle


Joined: 16 Apr 2008
Posts: 40
Reply with quote
I have two files.

TestRewrite.php:-

Code:
<?php error_reporting(E_ALL); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>Test Header Rewrite</title>
    </head>
    <body>
        <?php require_once('content1.php');?>
    </body>
</html>


and content1.php:-

Code:
<?php
    $insertGoTo = 'http://mywebserver';
    header(sprintf('Location: %s', $insertGoTo));
?>


When I run this in the debugger it throws an error:-

Quote:
Error: E_WARNING
Cannot modify header information - headers already sent by (output started at .....\TestRewrite.php:8)
at content1.php line 3


Yet when I run it on any of my web servers it works fine. See http://mywebserver/TempFiles/TestRewrite.php.

I can see the logic of why the error arises but how is it that all of the web servers I use process the file OK?

Charlie
View user's profileFind all posts by southcotSend private message


Joined: 23 Nov 2006
Posts: 48
Location: Netherlands
Reply with quote
look for output_buffering in your php.ini, in your live environment this is probably enabled

_________________
MV
View user's profileFind all posts by MacVatoSend private messageVisit poster's website


Joined: 16 Apr 2008
Posts: 40
Reply with quote
Thanks for such a prompt response. Yes output_buffering is set to 4096. Is the the issue, if so can I safely set this for the debugger version of PHP?

Charlie
View user's profileFind all posts by southcotSend private message


Joined: 23 Nov 2006
Posts: 48
Location: Netherlands
Reply with quote
you can do it in you test environment too, but it's better to use the build in php functions for output buffering

http://www.php.net/manual/en/function.ob-start.php

_________________
MV
View user's profileFind all posts by MacVatoSend private messageVisit poster's website


Joined: 20 May 2009
Posts: 11
Location: Germany
Reply with quote
just insert an explicit ob_start() in testrewrite.php/line1, so your script is portable.
View user's profileFind all posts by AbroSend private messageVisit poster's website


Joined: 16 Apr 2008
Posts: 40
Reply with quote
Thanks both. Since your post I've been looking over output buffering and it seems that it offers some really interesting possibilities but I shall have to take time out to experiment. I guess the ob_start() idea gives me time and keeps both the development and live environments running together.
View user's profileFind all posts by southcotSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
in fact, HTTP headers are always sent before HTTP body. PHP output buffer (ob_xxx) when enabled delays sending the body. Thus it leaves a chance to send headers after some ECHOs are done and some parts of HTTP body is sent to the output. Without buffer you get an error saying that you can't send headers because it's too late (or it's quite misleading equivalent in php's terms: Cannot modify header information)

I'd recommend to have all headers sent before the body. In this case you don't have to rely on ob status:

Code:
<?php error_reporting(E_ALL); ?>
<?php require_once('content1.php');?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>Test Header Rewrite</title>
    </head>
    <body>

    </body>
</html>



Also, please note that 'Location: xxx' header does not require any body, so you can have just
Code:
<?php
  error_reporting(E_ALL);
  require_once('content1.php');
?>

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 16 Apr 2008
Posts: 40
Reply with quote
Thanks Dmitri.

Charlie
View user's profileFind all posts by southcotSend private message
Header Rewrite Puzzle
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic