PHP Redirect Questions
Moderator: General Moderators
PHP Redirect Questions
Is there a more reliable way to do a refresh than simply echoing a meta refresh?
Re: PHP Redirect Questions
more reliable?Citizen wrote:Is there a more reliable way to do a refresh than simply echoing a meta refresh?
I dont understand...feyd wrote:Output buffering is used as a bandage, it is not a good solution to header problems. Page logic should be completed well before page content is being output.
I thought *all* php commands were run before any output could be sent.... it a preprocessor right? How could header() ever not work?
PHP is procedural. for example, if you output any HTML before php, the php will be parsed after the HTML is sent to the browser.
Since header must be sent before ANYTHING is sent to the browser, the above will not work.
Code: Select all
<html>
<body>
<p>this is some text</p>
<?php
if (something = something)
{
echo "this";
header (location: http://www.mysite.com);
}
?>- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
The HTML in the file is part of what it's processing. Whenever something tell it to output, it outputs. It's not a compiled language.Citizen wrote:I dont understand...feyd wrote:Output buffering is used as a bandage, it is not a good solution to header problems. Page logic should be completed well before page content is being output.
I thought *all* php commands were run before any output could be sent.... it a preprocessor right? How could header() ever not work?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You can use response header functions anywhere in your code. If you want them to work properly without errors, use them before sending output to the browser.Citizen wrote:Is there any way to do that after some phpcode is run or does header() have to be run before anything is echo'd?
Do not use output buffering to quiet your error message about headers already sent. That is a cheap bandaid that covers up bad code.
If that is the case, why doesn't anything show when you use the sleep() function until the sleep() is done? Shouldn't it display whatever you outputted first?guitarlvr wrote:PHP is procedural. for example, if you output any HTML before php, the php will be parsed after the HTML is sent to the browser.
Since header must be sent before ANYTHING is sent to the browser, the above will not work.Code: Select all
<html> <body> <p>this is some text</p> <?php if (something = something) { echo "this"; header (location: http://www.mysite.com); } ?>
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
sleep() has nothing to do with output. PHP sends its buffer to the web server once it
The web server may have a buffer too before it begins transmission to the requesting agent.
- reaches a certain size
- is told to
The web server may have a buffer too before it begins transmission to the requesting agent.