SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Timed Re-direct

Hybrid View

  1. #1
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Timed Re-direct

    Can you use PHP to make a "Timed Page Re-direct" like SitePoint has when you post a message?

    I am displaying a "transaction succeeded" message, and after maybe 15-20 seconds would like the screen to refresh and the user be taken to a Log-In page.

    How can I do that in PHP?

    Thanks,


    Amy

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't do it in PHP. PHP is on the server, so after sending output to the browser it has no further control. There is no persistent connection between client and server once the page has been sent.

    You could use a Javascript redirect. PHP can write JS to the page, if the destination is dynamic
    PHP Code:
    $redirect 'http://example.com/awesome-page.php';

    echo <<<EOD
    <script type="text/javascript">
    //redirect after 3 seconds
    setTimeout(
      function() { window.location = '
    $redirect'; },
      3000
    );
    </script>
    EOD; 

  3. #3
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Can't do it in PHP. PHP is on the server, so after sending output to the browser it has no further control. There is no persistent connection between client and server once the page has been sent.

    You could use a Javascript redirect. PHP can write JS to the page, if the destination is dynamic
    PHP Code:
    $redirect 'http://example.com/awesome-page.php';

    echo <<<EOD
    <script type="text/javascript">
    //redirect after 3 seconds
    setTimeout(
      function() { window.location = '
    $redirect'; },
      3000
    );
    </script>
    EOD; 

    So I could just paste that code into my web page and it will run automatically??

    (Assuming the user has JavaScript turned on.)



    Amy

  4. #4
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use the following script on my website. If you add the element with id of countdown, it will countdown whatever number you place in the element, and then redirect.

    Code:
    <script type = "text/javascript">
    	//<![CDATA[
                     var url = 'http://www.mysite.com/';
                     var element_id = "countdown";
                     var e = document.getElementById(element_id);
                     var cTicks = parseInt(e.innerHTML);
                     var timer = setInterval(function(){
    	              if( cTicks ){
    		               e.innerHTML = --cTicks;
    	              }else{
    		               clearInterval(timer);
    		               location = url;
    	              }
                     }, 1000);
          //]]>
    </script>


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
  •