[英]Multiple else if not working correctly PHP
I'm having an issue with a login script. 我的登录脚本有问题。 The rest of it works fine, but there is something odd happening here.
其余的一切正常,但是这里有些奇怪的事情发生。 The issue is that even though $IPCHK is returning true the elseif function does not execute.
问题是,即使$ IPCHK返回true,elseif函数也不会执行。 It only executes when I set $IPCHK to jibberish.
仅当我将$ IPCHK设置为乱码时才执行。 Any help would be great.
任何帮助都会很棒。 Thanks in advance
提前致谢
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";
}
}
In your Condition you have 在您的情况下,您有
elseif ($IPCHK == 'pending')
then 然后
elseif ($IPCHK == true)
the second else will never execute because $IPCHK == 'pending' means also that $IPCHK == true if you want to execute your second else you have to change the seconde condition to somethig like this 第二个将永远不会执行,因为$ IPCHK =='pending'也意味着$ IPCHK == true如果要执行第二个,则必须将seconde条件更改为诸如此类
elseif($IPCHK == 'done')
or simply use === like this 或像这样简单地使用===
elseif($IPCHK === 'pending')
then 然后
elseif($IPCHK === true)
Lamari Alaa is correct, and the relevant documentation entry on type juggling can help understand why. Lamari Alaa是正确的,有关类型变戏法的相关文档条目可以帮助理解原因。
The following script outputs: boolean = string
: 以下脚本输出:
boolean = string
:
$test = true;
if( $test == 'pending' ) {
echo 'boolean = string';
} else if ( $test ) {
echo 'boolean != string';
}
This is because the string 'pending' is being coerced into a boolean value before being compared to the boolean value true. 这是因为在将字符串“ pending”与布尔值true进行比较之前,将其强制转换为布尔值。 Since it evaluates as true, the first condition is taken.
由于它的评估结果为true,因此采用第一个条件。 Consider replacing
== 'pending'
to === 'pending'
考虑将
== 'pending'
替换为=== 'pending'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.