问题
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 :
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 ?.
回答1:
You could use header
. Something like this:
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.
Have a look at the PHP official website. And especially take notice to the following:
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.
回答2:
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.
For more information about the header function see: http://php.net/manual/en/function.header.php
回答3:
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
回答4:
<?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
回答5:
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>";
来源:https://stackoverflow.com/questions/8782131/on-this-event-go-to-this-url-else-go-here