Function header() working when it's not supposed to?!?

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
Zandro237
Forum Newbie
Posts: 4
Joined: Tue Jun 16, 2009 3:45 pm

Function header() working when it's not supposed to?!?

Post by Zandro237 »

Hey guys,

Quick breakdown of the scenario... I've got a form, and my goal in the form_submitted.php file is to process the information, output a message, and redirect to a home page about 5 seconds after outputting the message. Now I could've sworn I've always learned that nothing should to output to a page when header() is used. However, I tried it just to see what kind of error I'd get, and to my surprise, it worked perfectly. What am I missing here?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Function header() working when it's not supposed to?!?

Post by Eric! »

The rule is to always send headers first before outputing data.

Code: Select all

 
<html>
<?php
// This results in an error.
// The output above is before the header() call
header('Location: http://www.example.com/');
?> 
 
Zandro237
Forum Newbie
Posts: 4
Joined: Tue Jun 16, 2009 3:45 pm

Re: Function header() working when it's not supposed to?!?

Post by Zandro237 »

My bad, that's what I meant. So my point is I'm breaking that rule, but the code still works. Why?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Function header() working when it's not supposed to?!?

Post by Eric! »

Probably because you're using IE and they don't want granny's browser to say ERROR and stop working just because people don't follow good html rules.
Zandro237
Forum Newbie
Posts: 4
Joined: Tue Jun 16, 2009 3:45 pm

Re: Function header() working when it's not supposed to?!?

Post by Zandro237 »

I'm testing it on both IE and Firefox. It works on both.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Function header() working when it's not supposed to?!?

Post by Eric! »

same story, they are protecting people from their own ignorance. maybe the next version won't. Do you want your browser to break every time there is a missing closing html tag? Browers trade displaying errors every time there are missing tags or general sloppy html for functionality.
Zandro237
Forum Newbie
Posts: 4
Joined: Tue Jun 16, 2009 3:45 pm

Re: Function header() working when it's not supposed to?!?

Post by Zandro237 »

Great, thanks for your help.
Post Reply