943,513 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 763
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 23rd, 2009
0

is there another way?

Expand Post »
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
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009
May 23rd, 2009
1

Re: is there another way?

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?
PHP Syntax (Toggle Plain Text)
  1. <?ob_start()?> // use this at the beginning of the php code page
  2.  
  3. :
  4. :
  5. :
  6. :
  7. <?ob_flush()?>// use this at the end of page
I hope this should solve your problem.
Last edited by csharplearner; May 23rd, 2009 at 11:40 pm.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
csharplearner is offline Offline
99 posts
since Nov 2008
May 24th, 2009
0

Re: is there another way?

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 Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3. session_start();
  4. include("no_session.php"); // this checks for session authorization variable
  5.  
  6. if($_POST['new_password_1'] == $_POST['new_password_2']) {
  7.  
  8. if($_POST['old_password'] == $_SESSION['PASSWORD']) {
  9.  
  10. mysql_connect("localhost", "username", "password")
  11. or die("couldn't connect to server");
  12.  
  13. mysql_select_db("mydatabase")
  14. or die("couldn't select database");
  15.  
  16. $result = mysql_query("select password from member where login_name = '$_SESSION[USER]' and password = '$_SESSION[PASSWORD]';")
  17. or die("couldn't execute query");
  18.  
  19. $row = mysql_num_rows($result);
  20.  
  21. if($row > 0) {
  22.  
  23. mysql_query("update member set password = '$_POST[new_password_1]' where login_name = '$SESSION[USER]'")
  24. or die("couln't change password");
  25.  
  26. $_SESSION['PASSWORD'] = $_POST['new_password_1'];
  27. $_SESSION['STATUS'] = "Password Changed!";
  28.  
  29. // Redirect user to change password form
  30.  
  31. } else { // There was no match in the database
  32.  
  33. $_SESSION['STATUS'] = "Error - log out and try again";
  34.  
  35. // Redirect user to change password form
  36.  
  37. }
  38. } else { // Old password does not match session password entered origionall by the user.
  39.  
  40. $_SESSION['STATUS'] = "Old Password Incorrect!";
  41.  
  42. // Redirect user to change password form
  43.  
  44. }
  45.  
  46. } else {
  47.  
  48. $_SESSION['STATUS'] = "Your new passwords do not match!";
  49.  
  50. // Redirect user to change password form
  51.  
  52. }
  53. ?>
Last edited by Ezzaral; May 24th, 2009 at 12:28 am. Reason: fixed code tag
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009
May 24th, 2009
0

Re: is there another way?

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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
May 24th, 2009
0

Re: is there another way?

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)?
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009
May 24th, 2009
0

Re: is there another way?

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

Thank you so much...
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009
May 24th, 2009
0

Re: is there another way?

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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
May 25th, 2009
0

Re: is there another way?

i use
php Syntax (Toggle Plain Text)
  1. <?php ob_start("ob_gzhandler"); ?>
to buffer and gzip the output for faster transmission
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
May 25th, 2009
0

Re: is there another way?

Click to Expand / Collapse  Quote originally posted by almostbob ...
i use
php Syntax (Toggle Plain Text)
  1. <?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?
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009
May 25th, 2009
0

Re: is there another way?

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

addendum (reference to script.js.php
php version javascript file
php Syntax (Toggle Plain Text)
  1. <?php ob_start("ob_gzhandler");
  2. header ('content-type: text/javascript'); ?>
php version css file
php Syntax (Toggle Plain Text)
  1. <?php ob_start("ob_gzhandler");
  2. header ('content-type: text/css'); ?>
too is gzipped)
havent yet learned to enable gzip in apache
Last edited by almostbob; May 25th, 2009 at 8:42 pm.
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: using session variables to insert data to MYSQL Database
Next Thread in PHP Forum Timeline: Syntax error ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC