SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: header redirect

  1. #1
    SitePoint Zealot
    Join Date
    Apr 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    header redirect

    Is it possible to add a display message to the code below along the lines of "Redirecting now, please wait..."

    Code:
    <?php
    header("Location: http://www.example.com/");
    ?>

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    no. that command sends data to the browser that is used before any page data is sent and the renderer kicks in.

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't think so, because even if you use output buffering, the headers are sent first, according to the manual. You'd be better off using javascript by having your message as normal HTML, having the script sleep for a second or so and then doing its redirect.

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, both longneck and Raffles are incorrect: you can add a message below the header. In fact, this is recommended, as some poorly implemented clients may not redirect correctly. Just be aware that 99.999999% or more of your visitors will never see your message, they will simply be redirected immediately.

    If you want to display a message and delay the redirect, use a meta tag or a brief little JavaScript, although in both cases you'd also want to provide a link since some browsers don't or won't accept one or both of these methods (more so than won't respect the header).
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But Kromey, I don't see anything wrong with longneck's statement either. Header information is indeed sent before any of the html markup or other data. Even when you enable output buffering, it's header that goes out first. This is an essential requirement of HTTP and we cannot bend the rule.


    Yes you can add a message below header. But I don’t think that will be displayed by a good user agent, as kromey said. And you wouldn’t normally want anything beneath header redirect to display either. To tame bad user agents, you would normally follow the header redirect with exit().

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kromey
    you can add a message below the header. In fact, this is recommended, as some poorly implemented clients may not redirect correctly.
    100% correct. This is recommended, and even required by the http standard.

    Quote Originally Posted by rfc2616
    Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

  7. #7
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could also check to see if your stement is true use php to sleep then redirect:

    PHP Code:
    if ($myvariable) {

     echo 
    "Redirecting please wait";
     
    sleep(5); //time in seconds
     
    header("Location: http://www.example.com/");


    But, this all seems a bit pointless as you might aswell just redirect the user to there page without making them wait
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  8. #8
    SitePoint Zealot
    Join Date
    Apr 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kromey
    you can add a message below the header. In fact, this is recommended, as some poorly implemented clients may not redirect correctly. Just be aware that 99.999999% or more of your visitors will never see your message, they will simply be redirected immediately.
    Does the message need to be a proper html document with head, body etc or should it just be something like:
    Code:
    <?php
    header("Location: http://www.example.com/");
    echo('<a href="http://www.example.com/">Click here</a>');
    ?>
    If you want to display a message and delay the redirect, use a meta tag or a brief little JavaScript, although in both cases you'd also want to provide a link since some browsers don't or won't accept one or both of these methods (more so than won't respect the header).
    I've tried adding some javascript to delay the redirect while a message is displayed along with the link but again the javascript and html are never reached before the redirect happens.

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kailash Badu
    To tame bad user agents, you would normally follow the header redirect with exit().
    Is it not ok to carry on executing more code after the redirect?

    This is something I've wandered about actually. Currently I've got the redirect first then a database query which happens afterwards.

    The reason is the redirect is more important than the database query so if the database fails I still want the redirect to go ahead plus I would rather the user is redirected straight away and the server can carry on with the other stuff if there is a delay.

    So does the position in which the header() is placed make any difference or does the whole script finish up before the redirect actually takes place?

  10. #10
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    follow the header redirect with exit() and the rest of the code will never execute.

  11. #11
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Kansas City, MO
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    header("Refresh: 5; URL=\"destination.php\"");
    echo 
    "Redirecting in 5 seconds..."
    or...

    Code:
    <meta http-equiv="refresh" content="0;url=destination.php">
    Redirecting in 5 seconds...
    Not everything has to be complicated gents.

  12. #12
    SitePoint Zealot
    Join Date
    Apr 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kailash Badu
    follow the header redirect with exit() and the rest of the code will never execute.
    I meant without the exit.

  13. #13
    SitePoint Zealot
    Join Date
    Apr 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ZareMedia
    PHP Code:
    header("Refresh: 5; URL=\"destination.php\"");
    echo 
    "Redirecting in 5 seconds..."
    or...

    Code:
    <meta http-equiv="refresh" content="0;url=destination.php">
    Redirecting in 5 seconds...
    Not everything has to be complicated gents.
    I would like to do this without a meta refresh if possible. Is the first method a meta refresh? Thanks.

  14. #14
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    code.warrior, in regards to sending a redirect and then executing more code.
    yes, php will continue executing. for how long, may not be reliable without some additional work.

    if you explicitly intend for this to happen, i would recomend you call
    ignore_user_abort(true)

    im not sure how often or what causes php to check the connection. i know that when output is sent to the user, it seems the connection is checked. there may or may not be other times or events that cause it to be checked. if php notices the connection is no longer active, by default it will stop executing. thats what ignore_user_abort(true) is for, it tells php to keep going even if the connection is dropped.

    you said you wanted to make sure the redirect is sent right away, and THEN execute the other code. do something like this then.
    PHP Code:
    header(...);
    ignore_user_abort(true);
    echo 
    'redirecting too xxx';
    // flush output, forcing headers to be sent now.
    ob_flush();
    flush();
    // do other code now 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •