0

我正在尝试创建一个系统来存储最后登录的 IP,并将其与当前 IP 进行比较,然后稍后执行一些功能,但目前,如果不转到最后一个 else 语句,我无法比较它们。这是代码。

<?php

$userToPull = $session->userinfo['username'];

$query = "SELECT * FROM users WHERE username='$userToPull'";
            $result = mysql_query($query);      

            while($row = mysql_fetch_row($result)){

                    $userToShow = $row[25];
                    $IPtoVerify = $row[26];

                    }

$lastActivity = RelativeTime($userToShow);
$currIP = $_SERVER['REMOTE_ADDR'];

/*

Shows Partner Stuff

}elseif(!$session->isAdmin()){

echo "<div style='text-align:right;' id='homebox'";
echo "<b>Partner Total:</b> ".$database->getNumMembers()."<br>";
echo $database->num_active_users." partners logged in and ";
echo $database->num_active_guests." guests viewing the site.<br><br>";
echo "</div>";

*/


if(!$IPtoVerify == $currIP){

echo "<div style='text-align:right; background-color: #FAAAB3' id='homebox_partner'";
echo "<b>You are logged on from an unrecognized location.</b><br>";
echo "You will be sent a notification for security purposes.";
echo "<br>This location will automatically be remembered.";
echo "</div><br>";

}elseif($IPtoVerify == $currIP){

echo "<div style='text-align:right;' id='homebox_partner'";
echo "<b>You are logged on from a recognized location.</b><br>";
echo "Your IP is ".$_SERVER['REMOTE_ADDR'];
echo "<br>Your last login was approximately ".$lastActivity;
echo "</div><br>";

}else{

echo "<div style='text-align:right;' id='homebox_partner'";
echo "<b>An error has occurred.</b><br>";
echo "</div><br>";


}

?>

唯一不起作用的是if(!$IPtoVerify == $currIP){if 语句。

IP 以正常方式存储,echo 类似于:100.100.100.100。(正常时尚)

也许我没有正确比较它们,但它在过去对我有用。

4

3 回答 3

2

这段代码不符合你的想法:

if (!$IPtoVerify == $currIP) {

PHP 将其解释为:

if ((!$IPtoVerify) == $currIP) {

你会想把它写成:

if ($IPtoVerify != $currIP) {
于 2012-05-14T15:27:18.807 回答
0

尝试($IPtoVerify !== $currIP)代替(!$IPtoVerify == $currIP)

于 2012-05-14T15:25:47.970 回答
0
!$IPtoVerify == $currIP

方法

0==$currIP, 

因为它首先验证

`!$IPtoVerify` 

除非 $IPtoVerify 为 1,否则它总是返回 0。

添加额外的括号,例如

if(!($IPtoVerify == $currIP))...

解决问题。

于 2012-05-14T15:29:52.077 回答