#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171

    How can I use php header redirect and also print something on screen?


    Is it possible to use php header redirect and also print something on screen?

    Like Devshed login, show the welcome message then redirect them to another page after x seconds.

    Thanks
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,115
    Rep Power
    9398
    That's not a "header redirect". It's either JavaScript or, more likely, a "meta redirect". In the <head>
    Code:
    <meta http-equiv="Refresh" content="3; url=http://www.example.com" />
    That number in there is how many seconds to wait before redirecting.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171
    Originally Posted by requinix
    That's not a "header redirect". It's either JavaScript or, more likely, a "meta redirect". In the <head>
    Code:
    <meta http-equiv="Refresh" content="3; url=http://www.example.com" />
    That number in there is how many seconds to wait before redirecting.
    Hello, thank you for reply. I am already aware of this but I remember a while ago (I am not sure what exactly it was) Kicken or Dan (not sure which one) criticized the way I redirect pages using html meta tags in one of my websites.

    Anyways, is this proper:
    PHP Code:
    #Execute queries
    $success=true
    Show success message on screen
    Code:
    <meta http-equiv="refresh" content="2;url=index.php">
  6. #4
  7. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,118
    Rep Power
    1990
    Originally Posted by zxcvbnm
    I remember a while ago (I am not sure what exactly it was) Kicken or Dan (not sure which one) criticized the way I redirect pages using html meta tags in one of my websites.
    I would believe that this is because doing a HTTP redirect like that is not going to work 100% of the time. It's dependant on the browsers capabilities, and sometimes settings, so you can't depend on it. With the PHP header() call you know that the redirect command has been issued, so it's a lot less to worry about.

    If you want to show the user a message and still use the PHP redirection, store the message in a session variable, and echo it out on the next page. I've done this using a standard header file that performs those sort of checks and outputs messages like this as it needs to.
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171
    Originally Posted by Catacaustic
    If you want to show the user a message and still use the PHP redirection, store the message in a session variable, and echo it out on the next page.
    I like that. Thanks
  10. #6
  11. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487
    For re-directs on my sites I like to use the following:

    PHP Code:
    function do_pageRedirect($targetURL$processingMessage,$seconds) {
        echo 
    "<html>";
        echo 
    "<head>";
        echo 
    "<title>Page re-direct</title>";
        echo 
    "<meta http-equiv='refresh' content='{$seconds};url={$targetURL}'>";
        echo 
    "</head>";
        echo 
    "<body>";
        echo 
    "<b>{$processingMessage} ... </b><br>";
        echo 
    "This page should automatically re-direct you in {$seconds} second(s), if it doesn't then you can <b><a href='{$targetURL}'>click here</a></b> to jump manually.";
        echo 
    "</body>";
        echo 
    "</html>";

    An example use would be when a user logs in, if their provided credentials are OK then it would set the various session variables, call the re-direct function then die so that nothing else is done; something like this:

    PHP Code:
    $_SESSION['userlevel']=1;
    $_SESSION['useremail']="bob@nowhere.com";
    $_SESSION['note_to_user']="Your logon was OK";
    do_pageRedirect("index.php""Login OK",3);
    die(); 
    In 3 seconds, the page will re-direct to "index.php" (I usually set this to 0 seconds so the message just flashes up - if the browser doesn't support re-direction then the user has the option to 'click here to jump manually'). In "index.php" page I have the "note to user" displayed as such:

    PHP Code:
        if ($_SESSION['note_to_user']) {
        echo "<div class='note_to_user' id='note_to_user' style='background:#e7e676; text-align: center;' align='middle'>";
        echo "<p align='middle'><br><font class='header'><b>{$_SESSION['note_to_user']}</b><br><br></p>";
        echo "</div>";
    ?>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script language=javascript>
        <!--
        $("div.note_to_user").fadeOut(5000);
        //-->
        </script>
    <?php
            
    unset($_SESSION['note_to_user']);
        }

    This displays a fading DIV which displays the previously set $_SESSION['note_to_user'] and then clears it so if the page is refreshed then the same message won't re-appear.
    Hope you can find a use for it (or tear to bits and make better!)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by Catacaustic
    I would believe that this is because doing a HTTP redirect like that is not going to work 100% of the time. It's dependant on the browsers capabilities, and sometimes settings, so you can't depend on it. With the PHP header() call you know that the redirect command has been issued, so it's a lot less to worry about.

    ...
    I agree, but sometimes showing a message before a redirect is necessary, from the application usability point-of-view.

    This is circumvented by adding a notice within a page that if the browser does not redirect, the user is to click on the link. This is widely used in login forms, like in this forum, and also for site downloads, like in SourceForge.
  14. #8
  15. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    If you want to display a message before the redirect then you need to use a refresh redirect rather than a location redirect. You can do this either via a <meta> tag or still through the header() function.

    Code:
    header('Refresh: 5; url=http://blah/');
    or
    Code:
    <meta http-equiv="refresh" content="5; url=http://blah/">
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  16. #9
  17. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171
    Originally Posted by kicken
    Code:
    header('Refresh: 5; url=http://blah/');
    or
    Code:
    <meta http-equiv="refresh" content="5; url=http://blah/">
    Which one is a better practise?
  18. #10
  19. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by zxcvbnm
    Which one is a better practise?
    I'd go with the latter (does the former actually work) on the basis of separating the roles of the programmer and web designer. But that'd be my only reasoning for it.

IMN logo majestic logo threadwatch logo seochat tools logo