PHP Redirect Questions

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

Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

PHP Redirect Questions

Post by Citizen »

Is there a more reliable way to do a refresh than simply echoing a meta refresh?
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Re: PHP Redirect Questions

Post by mentor »

Citizen wrote:Is there a more reliable way to do a refresh than simply echoing a meta refresh?
more reliable?
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Seems you mixed 2 various things... Anyway what problem do you want to solve?
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

I need to automatically send a user to another page, but the meta refresh takes a long time to execute.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

header()
Is this you are looking for?
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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?
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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'.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

you can use ob_start() and redirect with header() in any part of the page.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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?
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply