How to Redirect Page in PHP after a few seconds wi

2019-01-23 03:51发布

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?

8条回答
祖国的老花朵
2楼-- · 2019-01-23 04:28

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楼-- · 2019-01-23 04:28

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.

查看更多
可以哭但决不认输i
4楼-- · 2019-01-23 04:36

Try use "refresh" header:

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

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

查看更多
倾城 Initia
5楼-- · 2019-01-23 04:38

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
查看更多
forever°为你锁心
6楼-- · 2019-01-23 04:42

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>
查看更多
Root(大扎)
7楼-- · 2019-01-23 04:43

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');

查看更多
登录 后发表回答