Page 1 of 1

using header to redirect when there is text on the page

Posted: Mon Jun 11, 2007 9:36 am
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.

Posted: Mon Jun 11, 2007 9:52 am
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]

Posted: Mon Jun 11, 2007 9:58 am
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>

Posted: Mon Jun 11, 2007 9:58 am
by suthie
ob_start() and ob_flush() did it

perfect! thanks!

Posted: Mon Jun 11, 2007 10:51 am
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."

Posted: Mon Jun 11, 2007 10:54 am
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.

Posted: Mon Jun 11, 2007 11:10 am
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.

Posted: Mon Jun 11, 2007 11:13 am
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)

Posted: Mon Jun 11, 2007 4:21 pm
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."