Page 1 of 2

PHP Redirect Questions

Posted: Fri Jun 29, 2007 12:52 pm
by Citizen
Is there a more reliable way to do a refresh than simply echoing a meta refresh?

Re: PHP Redirect Questions

Posted: Fri Jun 29, 2007 12:56 pm
by mentor
Citizen wrote:Is there a more reliable way to do a refresh than simply echoing a meta refresh?
more reliable?

Posted: Fri Jun 29, 2007 12:56 pm
by Gente
Seems you mixed 2 various things... Anyway what problem do you want to solve?

Posted: Fri Jun 29, 2007 1:04 pm
by Citizen
I need to automatically send a user to another page, but the meta refresh takes a long time to execute.

Posted: Fri Jun 29, 2007 1:06 pm
by Gente
header()
Is this you are looking for?

Posted: Fri Jun 29, 2007 1:34 pm
by Citizen
Is there any way to do that after some phpcode is run or does header() have to be run before anything is echo'd?

Posted: Fri Jun 29, 2007 1:45 pm
by Gente
So what logic do you want to get? You output the page and then redirect user to the other page? If it's so it's not 'PHP Redirect'.

Posted: Sat Jun 30, 2007 3:11 am
by miro_igov
you can use ob_start() and redirect with header() in any part of the page.

Posted: Sat Jun 30, 2007 7:15 am
by feyd
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.

Posted: Mon Jul 02, 2007 12:28 pm
by Citizen
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 dont understand...

I thought *all* php commands were run before any output could be sent.... it a preprocessor right? How could header() ever not work?

Posted: Mon Jul 02, 2007 12:35 pm
by guitarlvr
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.

Code: Select all

<html>
<body>
<p>this is some text</p>
<?php
if (something = something)
{
    echo "this";
    header (location: http://www.mysite.com);
}
?>
Since header must be sent before ANYTHING is sent to the browser, the above will not work.

Posted: Mon Jul 02, 2007 2:11 pm
by superdezign
Citizen wrote:
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 dont understand...

I thought *all* php commands were run before any output could be sent.... it a preprocessor right? How could header() ever not work?
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.

Posted: Mon Jul 02, 2007 3:55 pm
by RobertGonzalez
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?
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.

Do not use output buffering to quiet your error message about headers already sent. That is a cheap bandaid that covers up bad code.

Posted: Tue Jul 03, 2007 2:20 pm
by Citizen
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.

Code: Select all

<html>
<body>
<p>this is some text</p>
<?php
if (something = something)
{
    echo "this";
    header (location: http://www.mysite.com);
}
?>
Since header must be sent before ANYTHING is sent to the browser, the above will not work.
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?

Posted: Tue Jul 03, 2007 4:02 pm
by feyd
sleep() has nothing to do with output. PHP sends its buffer to the web server once it
  1. reaches a certain size
  2. is told to
If neither condition has been met, then no output will be sent to the web server. That does not mean output has not started however because to PHP, it has.

The web server may have a buffer too before it begins transmission to the requesting agent.