PHP Header redirect to Multiple URLs with a time gap

前端 未结 5 1839
北恋
北恋 2020-12-10 23:11

Can i use header() to redirect to multiple URLs with a time gap in between? Suppose i have url1 and url2. Now, what i want is that header first redirects to url1. Then say,a

相关标签:
5条回答
  • 2020-12-10 23:59

    No, you can't do that. Both headers are immediately sent to the browser, with the latter one overwriting the first one. You can add "Refresh: X" before the url, but it will only work for the last header sent (the URL that will get redirected to).

    Actually I don't see any way to accomplish this unless you control what is on url1 or use some tricks with frames.

    0 讨论(0)
  • 2020-12-11 00:08

    When you have been redirected to url1 you no longer posses the control to do another redirection.

    You need to specifically redirect from url1 to url2 on url1.

    You can also try iframe if you dont have control over url1

    0 讨论(0)
  • 2020-12-11 00:09

    That is not how it works. The redirect will take you to a new page...and THAT page would then have to do the next redirect. You should do something like:

    PAGE 1:

    <?php
    header("Location: url1");
    exit();
    ?>
    

    PAGE 2:

    <head>
    <meta http-equiv="refresh" content="5; URL=url2">
    </head>
    
    0 讨论(0)
  • 2020-12-11 00:12

    As pointed out by subirkumarsao, Martin and Edmunds, iframes is the way to go. Here is the final code:

    <html>
    <iframe src="url1" width="500" height="500"> </iframe>
    </html>
    
    <?php
    include('exp2.php');
    ?>
    

    Now exp2.php has a submit button(This appears at the bottom of frame) which takes me to next.php. And next.php has the code:

    <?php
    header("Location:url2");
    exit();
    ?>
    
    0 讨论(0)
  • 2020-12-11 00:13

    In the header:

    <script language="JavaScript">
    var time = null
    function move() {
    myiframe.location.href = 'url2';
    }
    </script>
    

    and this in the body:

    <body onload="timer=setTimeout('move()',2000)"> //where 2000 is 2 seconds
    <iframe id="myiframe" name="myiframe" src="url1"> </iframe>
    

    Cheers

    0 讨论(0)
提交回复
热议问题