Php 合并与用户登录状态相关的两条if-else语句
大家早上好 我需要一些帮助来合并与当前用户登录状态相关的两个if语句并创建第三个if语句 有3个州:Php 合并与用户登录状态相关的两条if-else语句,php,facebook,Php,Facebook,大家早上好 我需要一些帮助来合并与当前用户登录状态相关的两个if语句并创建第三个if语句 有3个州: 用户已使用facebook登录 连接选项 用户已使用网站登录 登录表单 用户未使用任何一种方式登录 我已经能够为1)和2)两个不能正常工作的条件语句起草了条件语句 它还有待完善 如果用户使用FacebookConnect选项登录,则条件语句不会在1)处停止,而是继续到2)处,从而产生错误未定义索引:uid 这是因为索引uid仅在用户使用网站表单登录时创建 我现在不知道该怎么做 我将非常感谢在推进
<?php
// if logged in using fb connect
if ($user) {
echo "fb";
// proceed knowing you have a logged in user who's authenticated
} else {
// proceed knowing you require user login and/or authentication
echo "this is shown to a user who has not logged in";
}
// if logged in using login form
if (isset($_SESSION['uid'])) {
// proceed knowing you have a logged in user who's authenticated
echo "kite"; }
// proceed knowing you require user login and/or authentication
else {
echo "this is shown to a user who has not logged in";
}
?>
您需要在
的else if
中检查uid
,而不是在另一个if
中。以下代码应修复此问题:
<?php
// if logged in using fb connect
if ($user) {
echo "fb";
// proceed knowing you have a logged in user who's authenticated
} // if logged in using login form
elseif (isset($_SESSION['uid'])) {
// proceed knowing you have a logged in user who's authenticated
echo "kite";
}
// proceed knowing you require user login and/or authentication
else {
// proceed knowing you require user login and/or authentication
echo "this is shown to a user who has not logged in";
}
?>
您需要在
的else if
中检查uid
,而不是在另一个if
中。以下代码应修复此问题:
<?php
// if logged in using fb connect
if ($user) {
echo "fb";
// proceed knowing you have a logged in user who's authenticated
} // if logged in using login form
elseif (isset($_SESSION['uid'])) {
// proceed knowing you have a logged in user who's authenticated
echo "kite";
}
// proceed knowing you require user login and/or authentication
else {
// proceed knowing you require user login and/or authentication
echo "this is shown to a user who has not logged in";
}
?>
好的,这里有一条裂缝:
if (isset($user)){ // use isset() to avoid undefined index warning ..
echo "fb";
} elseif($_SESSION['uid']){
echo "kite";
}else{
// Anonymous user
echo "this is shown to a user who has not logged in";
}
从技术上讲,如果您有一个保持登录状态的变量,则不需要最后一个else。如果您的last-ifelse返回false,则您知道用户是匿名的,但在本例中需要last-else,因此脚本不会在所有条件下回显匿名消息
请注意,登录到facebook的用户仍然可以设置$\u会话uid。
您需要将您想要的条件作为第一个要评估的条件,或者添加一个不允许同时设置fb和$\u会话的条件,我真的不知道您的意图是什么。好的,这里有一个裂缝:
if (isset($user)){ // use isset() to avoid undefined index warning ..
echo "fb";
} elseif($_SESSION['uid']){
echo "kite";
}else{
// Anonymous user
echo "this is shown to a user who has not logged in";
}
if($user || isset($_SESSION['uid'])){
echo $user?('fb'.(isset($_SESSION['uid'])?' AND kite':'')):'kite';
}else{
echo 'this is shown to a user who has not logged in';
}
从技术上讲,如果您有一个保持登录状态的变量,则不需要最后一个else。如果您的last-ifelse返回false,则您知道用户是匿名的,但在本例中需要last-else,因此脚本不会在所有条件下回显匿名消息
请注意,登录到facebook的用户仍然可以设置$\u会话uid。
您需要将您想要的条件作为第一个要评估的条件,或者添加一个不允许同时设置fb和$\u会话的条件,我真的不知道您的意图是什么。否则,如果不建议,最好使用elseif。摘自:注意:注意,只有在使用花括号时,elseif和elseif才会被视为完全相同。使用冒号定义if/elseif条件时,不能将else if分隔为两个单词,否则PHP将因分析错误而失败。@stefgosselin:+1。谢谢你增加了我的知识。我有一个不同的背景,所以一直在使用它,因为它一直在工作,因为我一直使用{}而不是:高兴我的朋友。Happy coding.else如果不推荐,最好使用elseif。摘自:注意:注意,只有在使用花括号时,elseif和elseif才会被视为完全相同。使用冒号定义if/elseif条件时,不能将else if分隔为两个单词,否则PHP将因分析错误而失败。@stefgosselin:+1。谢谢你增加了我的知识。我有一个不同的背景,所以一直在使用它,因为它一直在工作,因为我一直使用{}而不是:高兴我的朋友。快乐编码。用户不可能同时登录两个站点。用户不可能同时登录两个站点
if($user || isset($_SESSION['uid'])){
echo $user?('fb'.(isset($_SESSION['uid'])?' AND kite':'')):'kite';
}else{
echo 'this is shown to a user who has not logged in';
}