using header to redirect when there is text on the page

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
suthie
Forum Commoner
Posts: 68
Joined: Sat Jun 09, 2007 10:46 am

using header to redirect when there is text on the page

Post by suthie »

I am using this bit of code in a script:

Code: Select all

if($worked){
    echo "login successful. now redirecting... please wait";
    header( 'Location: sitepage' );
}
and it does not work. i read in a tutorial that header won't work if there is already text on the page. on my page there is text, and the text needs to be there. how can i make redirect work if there is text on my page?

there is also a bunch of php code before this to get postdata.
this is part of a php file that i am going to use to login and start a session then redirect users to another page.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

first line of code do:

Code: Select all

<?php
ob_start(); //starts a info buffer...


//and optional, but better to do on last line.
ob_flush(); //empty's the buffer, but its auto done at the end of the php script. it's just cleaner. :)
?>[syntax=php]
[/syntax]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Redirection means "no document for you here, request this one". Therefore the browser will not receive and display data after a "302 redirect" but request this new document immediately.
You might be interested in something like

Code: Select all

<html>
	<head>
		<meta http-equiv="refresh" content="5; URL=http://your.serv.er/script.php" />
		<title>...</title>
	</head>
	<body>
		login successful. now redirecting...
		<br />
		If you're not redirected within the next 10 seconds please use this link:
		<a href="http://your.serv.er/script.php">http://your.serv.er/script.php</a>
	</body>
</html>
Last edited by volka on Mon Jun 11, 2007 9:59 am, edited 1 time in total.
suthie
Forum Commoner
Posts: 68
Joined: Sat Jun 09, 2007 10:46 am

Post by suthie »

ob_start() and ob_flush() did it

perfect! thanks!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I'm surprised no one said this, but just don't echo anything before the redirection. It's not JavaScript, so there's no timer for the redirection, thus, no reason to display "please wait."
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

superdezign wrote:I'm surprised no one said this, but just don't echo anything before the redirection. It's not JavaScript, so there's no timer for the redirection, thus, no reason to display "please wait."
:?:
volka wrote:therefore the browser will not receive and display data after a "302 redirect" but request this new document immediately.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

volka wrote:
superdezign wrote:I'm surprised no one said this, but just don't echo anything before the redirection. It's not JavaScript, so there's no timer for the redirection, thus, no reason to display "please wait."
:?:
volka wrote:therefore the browser will not receive and display data after a "302 redirect" but request this new document immediately.
:P No no volka. Of course you explained why it doesn't work. ^_^

Besides, if he was having problems with header(), I'm sure he skipped over the meta redirection code pretty quickly.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Use meta redirection like volka suggested, if you absolutely have to have content displayed before the redirect. Output buffering is not a fix in this case as it only masks what appears to be bad planning in the way the script executes (in my opinion)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Everah wrote:Use meta redirection like volka suggested, if you absolutely have to have content displayed before the redirect. Output buffering is not a fix in this case as it only masks what appears to be bad planning in the way the script executes (in my opinion)
I agree. the output buffering is the only way to keep "the text needs to be there."
Post Reply