PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 07, 2019 4:46 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Wed Jun 26, 2002 11:00 am 
Offline
Site Admin

Joined: Thu Apr 18, 2002 3:14 pm
Posts: 1767
Location: Montreal, CA
Warning: Cannot add header information - headers already sent by (output started at file.php:23)

Have you ever gotten that warning message in PHP? Wonder what it's all about? Well, we can cover this very easily.

You are trying to send header information AFTER you have sent output to the browser.

Okay, lets give you an example of what will give that error.

Syntax: [ Download ] [ Hide ]
<?php echo "hello"; header("Location: http://www.phpcomplete.com");?>


This will raise an error, because you outputted hello to the browser via echo BEFORE you used the header() function. The header() function sends header information to the browser, so it makes sense the header inforation would come BEFORE the body, or the actual document. As soon as you send ANY content or data to the browser that isn't header information, you can't go back and send header information.

This would also give you an warning:

Syntax: [ Download ] [ Hide ]
  <?php header("Location: http://www.phpcomplete.com");?>


While it may seem like the above isn't sending any data other than header information, the problem is in the spaces BEFORE the opening PHP tag. That is a problem, because that is actual data.

People seem to get confused most often with header() because they think they can do something like this:

formTest.php
Syntax: [ Download ] [ Hide ]
<?php
// Form has been submitted...
if ( isset($_POST['submit']) ) {
    if ( empty($_POST['emailAddress'])) {
        echo "You didn't fill in your email address.";
        header("Location: formTest.php");
    } else {
        echo "Email was filled in.";
        header("Location: emailOkay.php");
    }
}
?>
<form method="post" action="formTest.php">
Enter an Email: <input type="text" name="emailAddress" />
<input type="submit" name="submit" value="Submit Email Address" />
</form> 


emailOkay.php
Syntax: [ Download ] [ Hide ]
Your email was okay.


This seems to be a common problem amongst newbies. For some reason, they think that they can output data to the browser, and then send header information. Unfortunately, they are confusing the HTML meta-refresh tag with the PHP header() function.

The above can be rewritten as such:

formTest.php
Syntax: [ Download ] [ Hide ]
<?php
// Form has been submitted...
if ( isset($_POST['submit']) ) {
    if ( empty($_POST['emailAddress']) {
        header("Location: emailOkay.php");
    } else {
        echo 'Email was not entered.';
    }
}?>
<form method="post" action="formTest.php">
Enter an Email: <input type="text" name="emailAddress" />
<input type="submit" name="submit" value="Submit Email Address" />
</form>
 


emailOkay.php
Syntax: [ Download ] [ Hide ]
Your email was okay.


The above example displays how easy it is to redirect using the header() function along with forms.

Another problem newbies face with the header() function is the infamous error that occurs in weird places that aren't echo'ed out, for example:

examplePage.php
Syntax: [ Download ] [ Hide ]
<?php
include "databaseInit.php";
// some normal non-display code here
header("Location: nextStep.php");
?> 


databaseInit.php
Syntax: [ Download ] [ Hide ]
 

<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
?> 

 


Here, the problem is in databaseInit.php, and it has to do with the blank lines. The blank lines before the code starts, and after the code end - both will cause problems. Remember, NO output whatsover, blank lines included!

Now, you may be feeling like that is a lot of work to use header(), but their is an easy solution to all of this: output buffering.

Output buffering is not only easy to use, but it works miracles. All the above examples could use output buffering, and would NOT cause errors, and would basically work. So you probably want to know how to use output buffering, so I will tell you.

In the above example with the database connections, we could do something like this:

examplePage.php
Syntax: [ Download ] [ Hide ]
<?php
include "databaseInit.php";
// some normal non-display code here
echo "Stuff happens here";
header("Location: nextStep.php");
// other code for other things...
include 'cleanUpModule.php';
?> 


databaseInit.php
Syntax: [ Download ] [ Hide ]
<?php
ob_start() // turn output buffering on
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
?> 


cleanUpModule.php
Syntax: [ Download ] [ Hide ]
<?php
ob_end_flush();
?>


Note: I never tried it, but I am told ob_end_flush() is called implicitly at the end of the PHP script, so you wouldn't even need to call ob_end_flush() at the end here if that is true.

Anyways, basically, output buffering doesn't send anything out to the browser until you tell it to, so you can have ouput and headers in whatever order you want. This is useful for many reasons, but even more useful in large projects when you don't know if someone is output something. The above would work, reordering the echo to happen after the header() function (of course, echo would never run, because header() would change the page location).

Well, that's it, I hope this clears up some problems. On a short note, everything here also applies to sessions and working with cookies. Once you send output, you can set a session or change cookies values. The methods here apply in the same manner though.

Enjoy =)


Last edited by jason on Sun Sep 21, 2003 7:32 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 12, 2002 10:01 am 
Offline
Forum Contributor
User avatar

Joined: Mon May 06, 2002 3:42 pm
Posts: 223
Location: Colorado
Thanks for that post! That must've taken a while to put together...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 20, 2002 2:52 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 29, 2002 3:46 am
Posts: 238
Location: Florida


Last edited by Benjamin on Mon May 11, 2009 2:05 am, edited 1 time in total.
Added [code=php] tags.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 29, 2002 2:17 pm 
Offline
DevNet Resident

Joined: Tue Apr 23, 2002 10:02 am
Posts: 1199
Location: Brisbane


Top
 Profile  
 
 Post subject: that's the ticket
PostPosted: Sun Oct 27, 2002 8:51 pm 
Offline
Forum Newbie

Joined: Sat Oct 26, 2002 6:39 pm
Posts: 4
Location: Newport News Virginia
Some of this I knew, but didn't know WHY. It's always in the details, isn't it?

Thanks for a well written mini-how-to!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 20, 2002 10:05 pm 
Offline
Forum Contributor
User avatar

Joined: Wed May 29, 2002 3:46 am
Posts: 238
Location: Florida
ob_start() and ob_end_flush() saved my life! :D


Top
 Profile  
 
PostPosted: Mon Dec 02, 2002 12:47 am 
Offline
Forum Contributor
User avatar

Joined: Thu Jul 11, 2002 1:35 am
Posts: 103
Location: Bengaluru (Bangalore) > Karnataka > India


Top
 Profile  
 
 Post subject: A workaround
PostPosted: Tue Dec 10, 2002 6:20 am 
Offline
Forum Newbie

Joined: Tue Dec 10, 2002 6:08 am
Posts: 3


Last edited by Benjamin on Mon May 11, 2009 2:07 am, edited 1 time in total.
Added [code=javascript] tags. Updated code to reflect current best practices.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 01, 2003 8:24 am 
Offline
Forum Commoner

Joined: Wed Jan 01, 2003 7:41 am
Posts: 28
actually I first got such an error message when using cookies.

the entry on cookies in the php manual included a something on it happening for header() though, so that was sort of killing 2 birds with 1 stone :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 13, 2003 8:29 am 
Offline
Forum Newbie
User avatar

Joined: Mon Jan 13, 2003 8:29 am
Posts: 3
Location: Fairbanks, Alaska


Last edited by Benjamin on Mon May 11, 2009 2:08 am, edited 1 time in total.
Changed [quote] to [code=php]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 13, 2003 10:19 am 
Offline
Forum Contributor
User avatar

Joined: Sun Dec 22, 2002 6:48 am
Posts: 132
Location: Worcester, MA
You are missing a ) it should be:

Syntax: [ Download ] [ Hide ]
if ( empty($_POST['emailAddress']) ){
 


Top
 Profile  
 
PostPosted: Wed Feb 12, 2003 1:46 am 
Offline
Forum Newbie
User avatar

Joined: Thu Feb 06, 2003 1:45 am
Posts: 20
Location: Perth, Western Australia
I've noticed that this phenomenon also occurs with setcookie. Everytime I want to get around it, I find that ob_start works just as well.
For instance, this won't work.
Syntax: [ Download ] [ Hide ]
<html>
<body>
<?php
 
echo "testing 1,2,...";
setcookie("testcookie", "test");
 
?>
</body>
</html>

But this will.
Syntax: [ Download ] [ Hide ]
 
<?php
ob_start();
?>
<html>
<body>
<?php
 
echo "testing 1,2,...";
setcookie("testcookie", "test");
 
?>
</body>
</html>


Hope that helps. :D


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 20, 2003 8:06 am 
Offline
Forum Newbie

Joined: Thu Mar 20, 2003 7:58 am
Posts: 3


Last edited by Benjamin on Mon May 11, 2009 2:09 am, edited 1 time in total.
Changed [code] to [code=php]


Top
 Profile  
 
 Post subject: Holy H Moly!!!
PostPosted: Wed Apr 30, 2003 10:56 pm 
Offline
Forum Commoner
User avatar

Joined: Tue Apr 29, 2003 3:13 pm
Posts: 34
Location: Houston, Texas


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 31, 2003 5:54 pm 
Offline
Forum Commoner
User avatar

Joined: Mon Aug 18, 2003 5:52 pm
Posts: 40
Location: Stockholm, Sweden
i don't know why but that ob_*() stuff has never worked for me!

oh well!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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 post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group