Hi,

I was wondering if there was another way to redirect a user to an other page other that the header("Location: URL") function.

My problem is that I am itterating through a buch of if statements and if i use the header function it gives me an error as the header has already passed.

Thanks

Hi,

I was wondering if there was another way to redirect a user to an other page other that the header("Location: URL") function.

My problem is that I am itterating through a buch of if statements and if i use the header function it gives me an error as the header has already passed.

Thanks

Did you use this?

<?ob_start()?> //  use this at the beginning of the php code page

:
:
:
: 
<?ob_flush()?>// use this at the end of page

I hope this should solve your problem.:)

Comments
This was just what i was looking for

Please forginve me if this sounds daft but i am new to PHP and programming in general.

Would that then allow me to use the header("Location: URL") function each time i get to the part where i need to redirect the user?

please see my code below

<?php
session_start();
include("no_session.php"); // this checks for session authorization variable

if($_POST['new_password_1'] == $_POST['new_password_2']) {
		
	if($_POST['old_password'] == $_SESSION['PASSWORD']) {

		mysql_connect("localhost", "username", "password")
			or die("couldn't connect to server");

		mysql_select_db("mydatabase")
			or die("couldn't select database");
			
		$result = mysql_query("select password from member where login_name = '$_SESSION[USER]' and password = 						'$_SESSION[PASSWORD]';")
				or die("couldn't execute query");
				
		$row = mysql_num_rows($result);
		
		if($row > 0) {
		
			mysql_query("update member set password = '$_POST[new_password_1]' where login_name = '$SESSION[USER]'")
				or die("couln't change password");	
							
			$_SESSION['PASSWORD'] = $_POST['new_password_1'];			
			$_SESSION['STATUS'] = "Password Changed!";
			
			// Redirect user to change password form
			
		} else {  // There was no match in the database
		
				$_SESSION['STATUS'] = "Error - log out and try again";
				
				// Redirect user to change password form
				
			}
	} else { // Old password does not match session password entered origionall by the user.
	
		$_SESSION['STATUS'] =  "Old Password Incorrect!";
		
		// Redirect user to change password form
		
	}
	
} else {

	$_SESSION['STATUS'] = "Your new passwords do not match!";
	
	// Redirect user to change password form

}
?>

I tripped on this a few times myself, early on.

As you have discovered, there's a strict order in which data are to be sent to the browser. Headers first, then the HTML. It isn't that 'the header has already been passed'; rather, it's that data other than headers have already been passed. The trick is to write the code such that the PHP emits no part of its generated output until you are sure it's safe to do so.

Yes, ob_start() will buffer the output until ob_flush() is executed. This means that you can continue to generate headers and build your HTML until the cows come home or shortly before the browser times out. Then you can flush the output and finish your page.

If it is not feasible or practical to use output buffering (if you can't change the other PHP code that transitions from headers to HTML, you may be able to employ another trick: ECMAScript (JavaScript), where you emit some ES code that replaces the current page's URL with that of the location to which you are sending the browser.

so all i would need to do is add ob_start to the begining of the page and then call it when i want to redirect along with the header(Location: URL)?

Love your work guys this is exactly what I was looking for...

Thank you so much...:icon_smile:

so all i would need to do is add ob_start to the begining of the page and then call it when i want to redirect along with the header(Location: URL)?

Add ob_start() before your PHP has generated any output (before it has generated any of <body>, <head>, <!DOCTYPE, <html>, etc.) You will be free to generate any headers you wish; headers are not buffered. Then call ob_end_flush() when you are done generating headers; might be best to call ob_end_flush() as the last thing you do before exitting PHP.

See ob_start() in the PHP manual.

i use

<?php ob_start("ob_gzhandler"); ?>

to buffer and gzip the output for faster transmission

Thanks for the tip. Is that all I need to do is put it in the () like you have done? or does this need to be in the ob_end_flush() as well?

<?php ob_start("ob_gzhandler"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
 <script language='javascript' type='text/javascript' src='/propertysel.js.php'></script>
 <Title>Add/Remove Resident - </Title>
	<?php define("_BB_DIR", "./bb/");
	define("COUNTER", _BB_DIR."mark_page.php");
	if (is_readable(COUNTER)) include_once(COUNTER);
	include ("./menu.php"); ?>
<p>Add/Remove Resident<br>
<FORM name="globe" METHOD="POST" ACTION="http://www.mysite.com/cgi-bin/bnbform.cgi">
	<INPUT TYPE="HIDDEN" NAME="outputfile" VALUE="mailapps"><INPUT TYPE="HIDDEN" NAME="countfile" VALUE="countmailapps">
	<INPUT TYPE="hidden" NAME="submit_to" VALUE="enquiries@mysite.com">
<!--  
bla bla  
-->
to confirm this application</p>
</body></html><?php ob_flush(); ?>

addendum (reference to script.js.phpphp version javascript file

<?php ob_start("ob_gzhandler");
header ('content-type: text/javascript'); ?>

php version css file

<?php ob_start("ob_gzhandler"); 
header ('content-type: text/css'); ?>

too is gzipped)
havent yet learned to enable gzip in apache

This question has already been answered. Start a new discussion instead.