| | |
is there another way?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
•
•
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
PHP Syntax (Toggle Plain Text)
<?ob_start()?> // use this at the beginning of the php code page : : : : <?ob_flush()?>// use this at the end of page
Last edited by csharplearner; May 23rd, 2009 at 11:40 pm.
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
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 Syntax (Toggle Plain Text)
<?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 } ?>
Last edited by Ezzaral; May 24th, 2009 at 12:28 am. Reason: fixed code tag
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
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.
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.
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
•
•
•
•
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)?
See ob_start() in the PHP manual.
i use to buffer and gzip the output for faster transmission
php Syntax (Toggle Plain Text)
<?php ob_start("ob_gzhandler"); ?>
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
•
•
•
•
i useto buffer and gzip the output for faster transmissionphp Syntax (Toggle Plain Text)
<?php ob_start("ob_gzhandler"); ?>
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?
@ I'm gonna live forever, or die trying.
@ A wise man once told me, in order to understand recursion, you first must understand recursion.
@ A wise man once told me, in order to understand recursion, you first must understand recursion.
php Syntax (Toggle Plain Text)
<?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.php
php version javascript filetoo is gzipped)php version css filephp Syntax (Toggle Plain Text)
<?php ob_start("ob_gzhandler"); header ('content-type: text/javascript'); ?>php Syntax (Toggle Plain Text)
<?php ob_start("ob_gzhandler"); header ('content-type: text/css'); ?>
havent yet learned to enable gzip in apache
Last edited by almostbob; May 25th, 2009 at 8:42 pm.
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
![]() |
Other Threads in the PHP Forum
- Previous Thread: using session variables to insert data to MYSQL Database
- Next Thread: Syntax error ?
| Thread Tools | Search this Thread |
# 5.2.10 action address apache api array auto autoincrement beginner binary broken cakephp checkbox class classes cms code cron curl database date dehasher destroy display dissertation domain dynamic echo echo$_get[x]changingitintovariable... email error errorlog fatalerror file files folder form forms function functions google href htaccess html if-else image images include insert ip javascript joomla legislation limit link load login mail masterthesis menu mlm multiple mysql mysqlquery oop open paypal pdf persist php popup problem query radio random record recursion remote script search server sessions sms sockets source space sql syntax system table tutorial update upload url validator variable video web youtube






