Thread: Redirection

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

    Redirection


    Hi to all:

    This is my first time on this forum which reflects my status as a new PHP coder. After many frustrating hours fighting with the "meta tag" and its refresh method it finally revealed to me that the event did not take place until after the code was exited and if there was more than "one tag" the last one got the prize - talk about stochastic. I also tried the "header" solution but that wipes out any preceding "alert" popups which is what I am trying, ultimately to do. If I encounter a user error I want to exit the page and present the message and then cycle back to the main page. The key point here is exit at that point!! Any suggestions would be greatly appreciated. I should mention that I have done a workaround with "if then else" logic but I would really like to know if such a "redirection-immediately exit" tool is available. Again, thanks in advance.
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    header() will make the browser do nothing but redirect. It won't show HTML, it won't run Javascript. In fact you shouldn't be outputting anything at all if you redirect that way*.

    The best solution, in general, is to immediately redirect to whatever page you want to redirect to and show the error message there. No interstitial page - it's just confusing.

    That aside you can
    a) Use Javascript to redirect, since you're using it for the message anyways.
    b) Use a <meta> to redirect after a few seconds, giving them time to read messages and whatever.
    c) Give the user a link to click so they can go back to whatever page on their own time.

    * Besides maybe a message (that users probably won't see) that tells them they're being redirected, but that's only for browsers that don't understand redirections well enough and they've all but died out now.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by requinix
    header() will make the browser do nothing but redirect. It won't show HTML, it won't run Javascript. In fact you shouldn't be outputting anything at all if you redirect that way*.

    The best solution, in general, is to immediately redirect to whatever page you want to redirect to and show the error message there. No interstitial page - it's just confusing.

    That aside you can
    a) Use Javascript to redirect, since you're using it for the message anyways.
    b) Use a <meta> to redirect after a few seconds, giving them time to read messages and whatever.
    c) Give the user a link to click so they can go back to whatever page on their own time.

    * Besides maybe a message (that users probably won't see) that tells them they're being redirected, but that's only for browsers that don't understand redirections well enough and they've all but died out now.
    Thanks for your response requinix. Please see my test case below that I will base my discussion on.

    Calling code:
    <!doctype html>
    <?php
    // trying to understand PHP

    echo '<script language="javascript" type="text/javascript">
    alert(\'' . "in start of Test" . '\');
    </script>';

    header( 'Location: http://localhost/socialios/test2.php' ) ;
    // echo '<META http-equiv="refresh" content="1;URL=test2.php"></meta>';

    echo '<script language="javascript" type="text/javascript">
    alert(\'' . "at end of Test" .'\');
    </script>';
    ?>
    </body>
    </html>
    Called Code:
    <!doctype html>
    <?php
    // trying to understand PHP

    echo '<script language="javascript" type="text/javascript">
    alert(\'' . "in start of Test2" . '\');
    </script>';

    echo '<META http-equiv="refresh" content="1;URL=testLogin.htm"></meta>';


    ?>
    </body>
    </html>

    If I activate the "header method" then none of the alerts are displayed in the test.php module but the alert in test2.php is. The fact that the second alert in test.php does not display is expected and appreciated - its means the exit was made at point of execution but why didn't the first alert execute. This cancellation of previous alerts has a rather large scope as well. It will reach back pass an if check that the header statement is a "member" of. This is what perplexes me concerning the use of header. I agree with you completely to display the "informing" alert in the called code.
    If I activate the "meta tag" instead then both alerts in the test.php code are executed and then the alert in test2.php is executed as well. This implies that the meta tag is not "executed" until the php code is exited. Is there some kind of a "queuing system" being used? I have also noticed that if you have multiple meta tags "stacked" then the last one is executed and all previous members in the stack are thrown away? Just trying to resolve the "nature of the beast". Again thanks for your expertise.
    Respectully,
    Ideprize
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    There is no queueing system, but code is executed in a specific order.

    1. The browser makes an HTTP request to the server
    2. The server executes PHP code to handle the response
    3. The server sends the output from the PHP code to the browser
    4. The browser renders the output

    All of your PHP code is executed before any of your JavaScript or HTML, because the execution of JavaScript and the display of HTML are both part of step 4.

    Calling the header() function in PHP does not end execution of the PHP script. This is critical to understand, because if you do something like:
    Code:
    if user is not authorized to delete resource:
      header redirect to another page
    
    run query to delete resource
    The resource will still be deleted even if the user is not authorized to delete it. In order to make execution of the PHP script end, you must call exit or die.

    Calling header() sets an HTTP header in the response sent at step 3. As their name implies, headers are sent at the beginning of the response, before the body of the page, and they are processed by the browser before any JavaScript or HTML is executed. If the server has output buffering turned off (which is the default), you cannot call header() successfully after having output any content (ex: using print or echo) because outputting content signifies the end of the headers section of the HTTP response.

    The HTTP 'location' header (the one used to perform redirects using the header() function) is interpreted by most browsers as meaning that the browser should not render any of the content sent by the server, and should instead immediately request and display the new URL. However, the server still sends the content output by the PHP server to the browser, and the browser could choose to display this content instead of redirecting the user if it wanted to.

    I would not rely on any consistency to the way multiple meta redirect tags are handled. You simply should not output more than one meta redirect tag on a page because you can't guarantee how it will be handled.

    However, basically what it comes down to is that you cannot use a header redirect and display an alert in the same HTTP request.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by E-Oreo
    There is no queueing system, but code is executed in a specific order.

    1. The browser makes an HTTP request to the server
    2. The server executes PHP code to handle the response
    3. The server sends the output from the PHP code to the browser
    4. The browser renders the output

    All of your PHP code is executed before any of your JavaScript or HTML, because the execution of JavaScript and the display of HTML are both part of step 4.

    Calling the header() function in PHP does not end execution of the PHP script. This is critical to understand, because if you do something like:
    Code:
    if user is not authorized to delete resource:
      header redirect to another page
    
    run query to delete resource
    The resource will still be deleted even if the user is not authorized to delete it. In order to make execution of the PHP script end, you must call exit or die.

    Calling header() sets an HTTP header in the response sent at step 3. As their name implies, headers are sent at the beginning of the response, before the body of the page, and they are processed by the browser before any JavaScript or HTML is executed. If the server has output buffering turned off (which is the default), you cannot call header() successfully after having output any content (ex: using print or echo) because outputting content signifies the end of the headers section of the HTTP response.

    The HTTP 'location' header (the one used to perform redirects using the header() function) is interpreted by most browsers as meaning that the browser should not render any of the content sent by the server, and should instead immediately request and display the new URL. However, the server still sends the content output by the PHP server to the browser, and the browser could choose to display this content instead of redirecting the user if it wanted to.

    I would not rely on any consistency to the way multiple meta redirect tags are handled. You simply should not output more than one meta redirect tag on a page because you can't guarantee how it will be handled.

    However, basically what it comes down to is that you cannot use a header redirect and display an alert in the same HTTP request.
    E-oreo, thank you quantumly. That was an education! Being a mathematician by education I simply love a definitive sequence of events that pieces together a rather convoluted collection of stochastic processes into a predictable ordered set and that is exactly what you did - thanks again!
    Respectfully,
    Ideprize
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    For the sake of absolute accuracy I should clarify that steps 2, 3 and 4 do sometimes overlap; but not in a way that should affect the way you write your code. Essentially in 99%+ of situations you can program as if they happen strictly sequentially.

    The server will never start step 3 before the PHP script starts delivering the content body, but it might start step 3 before PHP finishes delivering the content body. This is the primary reason that header() cannot be called successfully after you start to output the content body.

    Similarly, some browsers may start step 4 before they've received all of the content body from the server, so conceivably both steps 2 and 3 could still be active at that point.

    However, because you cannot change what has already been sent to the browser, and because the connection is uni-directional at that point (the browser cannot send additional data as part of the same HTTP request) it rarely matters that there is overlap here. In practice complications only arise from this if you are delivering very large content bodies (megabytes in size) or have very long-running PHP scripts (dozens of seconds).
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by E-Oreo
    For the sake of absolute accuracy I should clarify that steps 2, 3 and 4 do sometimes overlap; but not in a way that should affect the way you write your code. Essentially in 99%+ of situations you can program as if they happen strictly sequentially.

    The server will never start step 3 before the PHP script starts delivering the content body, but it might start step 3 before PHP finishes delivering the content body. This is the primary reason that header() cannot be called successfully after you start to output the content body.

    Similarly, some browsers may start step 4 before they've received all of the content body from the server, so conceivably both steps 2 and 3 could still be active at that point.

    However, because you cannot change what has already been sent to the browser, and because the connection is uni-directional at that point (the browser cannot send additional data as part of the same HTTP request) it rarely matters that there is overlap here. In practice complications only arise from this if you are delivering very large content bodies (megabytes in size) or have very long-running PHP scripts (dozens of seconds).
    I understand - an ordered set with "slide". Again, thanks E-Oreo.
    Respectfully,
    Ideprize
  14. #8
  15. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0
    Use this code to redirect the page .
    header( 'Location: http://localhost/socialios/test2.php' ) ;

IMN logo majestic logo threadwatch logo seochat tools logo