php 需要在不使用函数头的情况下重定向 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18206797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:09:37  来源:igfitidea点击:

need to redirect url without using the function header

phpurl-redirection

提问by Ricardo Alves

I need to redirect my page to another, but I can't use the headerfunction, is there any other way to do it?

我需要将我的页面重定向到另一个页面,但我无法使用该header功能,还有其他方法可以做到吗?

What a I need to do is something like this:

我需要做的是这样的:

if (test){
   ---do something---
   redirect
} else {
   return false
}

Thanks

谢谢

回答by Parfait

if (test){
   ---do something---
   die("<script>location.href = 'http://www.google.com'</script>");
} else {
   return false;
}

回答by Deepu

A meta-redirect

元重定向

    if (test){
       //do something
       echo '<META HTTP-EQUIV="Refresh" Content="0; URL=yourpage.php">';//This causes the browser to open the new page after 0 seconds, i.e immediately.
    }  else {
       return false;
    } 

回答by Hamender

if (test){
  echo "<script type='text/javascript'>
window.onload = function () { top.location.href = '" . $url . "/page.php#contact'; };
</script>";
} else {
   return false
}

回答by Shubham Kumar

if (test){
   ---do something---
  $URL="http://yourwebsite.com/";
  echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
  echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
} else {
   return false;
}

If you're wondering that why I have used both Meta tag and JavaScript to Redirect, then the answer is very simple.

如果您想知道为什么我同时使用Meta 标记和 JavaScript 来 Redirect,那么答案很简单。

If JavaScript is Disabled in the Browser, then meta tag will redirect the page.

如果浏览器中禁用了 JavaScript,则元标记将重定向页面。