How to Redirect Page in PHP after a few seconds wi

2019-01-23 04:25发布

问题:

It seems that it is not advisable to use

<meta http-equiv=REFRESH CONTENT=3;url=url>

for redirects but instead use

header('Location: url')

However, I would like to show the user some message and allow them some time to read it before redirecting. Is there a way to do it without meta?

回答1:

Try use "refresh" header:

header('Refresh: 3;url=page.php');

Also, you can look at this Question Refresh HTTP Header.



回答2:

There is nothing wrong with using the meta refresh tag.

<meta http-equiv="refresh" content="5;URL='http://example.com/'" />

That tag says wait 5 seconds and redirect to example.com. This tag isn't a problem unless users are on IE6 and it still works, just breaks the history buttons.

Using JavaScript is an option, but make sure you include a link saying "If you are not automatically redirected, please click here". You should actually include that link either way.



回答3:

php way to set header, will redirect you to test.php in 5 seconds:

header( "refresh:5;url=test.php" );

call before any actual output is sent.

And in javascript:

setTimeout(function () {
   window.location.href= url; // the redirect goes here
},5000); // 5 seconds


回答4:

Header tags are sent on page load, to the browser, so that it can quickly redirect the user to the desired page without bothering to render it or even load it into the history. As such, you can't call a redirect once the page has already loaded, as the headers have already been dealt with.

You can instead perform this with:

header( "refresh:5;url=wherever.php" );

Which basically sets the <meta> tag in the headers of the page itself, meaning you don't need to write the tag out.



回答5:

By what you guys are saying, theoretically this should work then:

URL: http://www.example.com/ticketgen/index.php?success=1&redir=1

    <?php
 $myredir = ($_GET['redir']);
    if ($myredir == 1)
    {
        header( "refresh:5;url=http://www.example.com/ticketgen/" );
    }
?>

But it does nothing. I also have it at the VERY TOP of the page so it can send the headers.

it doesn't work in Firefox i just found out.



回答6:

You can do it with a small piece of javascript:

<script type="text/javascript" language="JavaScript">location.href = 'otherpage.php';</script>

Of course, this will depend on the person having JavaScript enabled.

Obviously, to set the delay you can use something like setTimeout:

<script type="text/javascript" language="JavaScript">
    setTimeout(function () {
                      location.href = 'stackoverflowhelp.php'; 
               }, 5000);
</script>


回答7:

I think really the best way is header("Refresh: 10;url=../index.php"); Like what i've done with my work.



回答8:

https://codingislove.com/redirect-pages-php/

check out the above article, where they clearly explained about how to redirect the pages in PHP by setting time.

Redirecting code without time set: header('location:URL ADDRESS');

Redirecting code with three seconds time set: header('refresh:3; url=URL ADDRESS');