is there another way?

Thread Solved

Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

is there another way?

 
0
  #1
May 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 99
Reputation: csharplearner is an unknown quantity at this point 
Solved Threads: 3
csharplearner's Avatar
csharplearner csharplearner is offline Offline
Junior Poster in Training

Re: is there another way?

 
1
  #2
May 23rd, 2009
Originally Posted by leviathan185 View 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
Did you use this?
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

Re: is there another way?

 
0
  #3
May 24th, 2009
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

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: is there another way?

 
0
  #4
May 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

Re: is there another way?

 
0
  #5
May 24th, 2009
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)?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

Re: is there another way?

 
0
  #6
May 24th, 2009
Love your work guys this is exactly what I was looking for...

Thank you so much...
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: is there another way?

 
0
  #7
May 24th, 2009
Originally Posted by leviathan185 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,321
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 161
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: is there another way?

 
0
  #8
May 25th, 2009
i use
  1. <?php ob_start("ob_gzhandler"); ?>
to buffer and gzip the output for faster transmission
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

Re: is there another way?

 
0
  #9
May 25th, 2009
Originally Posted by almostbob View Post
i use
  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?
@ I'm gonna live forever, or die trying.

@ A wise man once told me, in order to understand recursion, you first must understand recursion.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,321
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 161
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: is there another way?

 
0
  #10
May 25th, 2009
  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
  1. <?php ob_start("ob_gzhandler");
  2. header ('content-type: text/javascript'); ?>
php version css file
  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.
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC