php 在这个事件去这个网址,否则去这里
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8782131/
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
On this event go to this url , else go here
提问by Iain Simpson
I am trying to write an if statment in php but I am not sure how to redirect the user to another url on a condition. What I am trying to do is :
我正在尝试在 php 中编写一个 if 语句,但我不确定如何在条件下将用户重定向到另一个 url。我想做的是:
if $member = $notmember
go here index.php?option=com_jumi&view=application&fileid=6&Itemid=174
else
go here index.php?option=com_jumi&view=application&fileid=6&Itemid=124
I am not sure how to redirect to another url though ?.
我不确定如何重定向到另一个网址?
回答by insumity
You could use header
. Something like this:
你可以使用header
. 像这样的东西:
if ($member == $notmember)
header("Location: index.php?option=com_jumi&view=application&fileid=6&Itemid=174");
else
header("Location: index.php?option=com_jumi&view=application&fileid=6&Itemid=124");
If after the else
, you have more code, you should use exit()
in order to prevent of your code to keep executing.
如果在 之后else
,你有更多的代码,你应该使用 exit()
以防止你的代码继续执行。
Have a look at the PHP official website. And especially take notice to the following:
看看 PHP 官方网站。尤其要注意以下几点:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
请记住,header() 必须在发送任何实际输出之前调用,无论是通过普通的 HTML 标记、文件中的空行还是来自 PHP。
回答by Henridv
You can use header():
您可以使用标题():
<?php
if ($member == $notmember) {
header("Location: http://xx.com");
exit;
} else {
header("Location: http://yy.com");
exit;
}
?>
Don't forget exit, otherwise the rest of you php script will be executed.
不要忘记exit,否则将执行其余的 php 脚本。
For more information about the header function see: http://php.net/manual/en/function.header.php
有关 header 函数的更多信息,请参见:http: //php.net/manual/en/function.header.php
回答by Niet the Dark Absol
if( $member == $notmember) // use TWO equals signs, otherwise you're just assigning
header("Location: index.php.......");
else
header("Location: index.php.......");
exit; // stop script execution
回答by Iain Simpson
<?php if ($member == $notmember) {?>
<script type="text/javascript">window.location = "index.php?option=com_jumi&view=application&fileid=6&Itemid=174"</script>
<?php } else {?>
<script type="text/javascript">window.location = "index.php?option=com_jumi&view=application&fileid=6&Itemid=124"</script>
<?php } ?>
this is php w/ js solution
这是带有 js 的 php 解决方案
回答by Mehran
you can use this too:
你也可以使用这个:
if ($member == $notmember)
echo "<script>window.location = 'index.php?option=com_jumi&view=application&fileid=6&Itemid=124";
else
echo "<script>window.location = 'index.php?option=com_jumi&view=application&fileid=6&Itemid=124'</script>";