我遇到登录脚本问题。其余的工作正常,但这里有一些奇怪的事情发生。问题是即使$ IPCHK返回true,elseif函数也不会执行。它只在我将$ IPCHK设置为乱码时执行。任何帮助都会很棒。提前致谢
if ($Numrows == 0)
{
if ($Fail >= 3)
{
$Connection = connectToDb();
//return true, false,pending
$IPCHK = checkIP();
$IPCHK = true; //forcing it to be true and still broke
//If no ip id there
if($IPCHK == false)
{
$IP = getIP();
$Query = "INSERT INTO ip VALUES ('','$IP',Now())";
mysqli_query($Connection, $Query)
or die(error(mysqli_error($Connection)));
echo "You have failed to login too many times";
echo "<br />Please <a href='login.php'>try again</a> later.";
$Lock = true;
}
//If ip is there but timer is not up
elseif ($IPCHK == 'pending')
{
echo "You have failed to login too many times";
echo "<br />Please <a href='login.php'>try again</a> later.";
$Lock = true;
}
//Timers Up
elseif ($IPCHK == true) //here does not execute when it returns true
{
$_SESSION['FailedLogin'] = 0;
$Lock = false;
}
else
{
error("End of if check");
}
}
else
{
$Fail = 3 - $Fail;
$_SESSION['FailedLogin'] = $_SESSION['FailedLogin'] + 1;
$Error = $Error."<br />You have ".$Fail." attempts remaining";
}
}
答案 0 :(得分:0)
在你的条件下你有
elseif ($IPCHK == 'pending')
然后
elseif ($IPCHK == true)
其他第二个永远不会执行因为$ IPCHK =='pending'也意味着$ IPCHK == true 如果你想执行你的第二个,你必须将seconde条件更改为像这样的
elseif($IPCHK == 'done')
或简单地使用===喜欢这个
elseif($IPCHK === 'pending')
然后
elseif($IPCHK === true)
答案 1 :(得分:0)
Lamari Alaa是正确的,type juggling上的相关文档条目有助于理解原因。
以下脚本输出:boolean = string
:
$test = true;
if( $test == 'pending' ) {
echo 'boolean = string';
} else if ( $test ) {
echo 'boolean != string';
}
这是因为在与布尔值true进行比较之前,字符串'pending'被强制转换为布尔值。由于它的评估结果为真,因此采用了第一个条件。考虑将== 'pending'
替换为=== 'pending'