PHP - Delay on redirect

后端 未结 3 595
借酒劲吻你
借酒劲吻你 2020-12-30 15:40

I\'m using the following code to use as a form of nulling referring script and it works perfectly but it just redirects them straight to the target URL.

How would I

相关标签:
3条回答
  • 2020-12-30 15:43

    What about using sleep()?

    function method1(...) {
    
    sleep(5);
    
    ... rest of the code
    

    Note however that it is more recommended to use Vahe Shadunts's answer, which uses header() instead.

    0 讨论(0)
  • 2020-12-30 15:52

    The refresh header does the job but I'd like to highlight some potential issues:

    • It is not specified in the HTTP standard. Wikipedia says:

      Proprietary and non-standard: a header extension introduced by Netscape and supported by most web browsers.

      But it has been around for almost 20 years now and I don't know of any browser that does not support it (could not find a reference though)

    • Some browsers do not use the cache on a page redirected with refresh. It has been demonstrated for Internet Explorer here: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/meta-refresh-causes-additional-http-requests.aspx and I coud reproduce it on Firefox. Chrome does not have this issue.

    Alternative: JavaScript

    You can add a JavaScript on the intermediate page, that opens a new page after X seconds. Add this at the bottom of the page to redirect to http://www.example.com/target after 5 seconds:

    <script type="text/javascript">
        window.setTimeout(function() {
            window.location.href='http://www.example.com/target';
        }, 5000);
    </script>
    

    Combination

    As a bonus, you can fall back to the refresh header if JS is disabled, using the meta directive http-equiv that tells the browser to act as if a certain HTTP header has been sent. Because it is part of the HTML source, you can wrap it in a <noscript> element. Add this to your <head> additionally to the JavaScript above:

    <noscript>
        <meta http-equiv="refresh" content="5;url=http://www.example.com/target" />
    </noscript>
    

    Now, the page redirects with JavaScript if available for the best performance, and uses refresh otherwise.

    0 讨论(0)
  • 2020-12-30 16:07

    You can send php header with timeout refresh. http://php.net/manual/en/function.header.php

    <?php 
      header( "refresh:5; url=wherever.php" ); 
    ?>
    
    0 讨论(0)
提交回复
热议问题