1

所以我正在根据当前会话检查会话 IP。我已经查看了可能有我答案的问题,但没有一个答案。

发生的情况是,每次连接时,我都会收到“您的 IP 不匹配...”

<?php
if (isset($_SESSION['ip']) and (strcmp($_SESSION['ip'], md5($_SERVER['REMOTE_ADDR'])))){
    echo "Your IP doesn't match your session, your IP: ". $_SERVER['REMOTE_ADDR'] ." and your session IP: ". $_SESSION['ip']. ". <a href='/scripts/signout.php'>Click here to log out.</a>";
    die;
}
?>

我知道要使用 HTTPX,并且在我解决这个问题后会实现它。$_SESSION['ip'] 也是 md5(当前 IP)

4

2 回答 2

2

您正在比较 ip 地址的会话 ip 和 md5,因此您可能需要:

<?php
if (isset($_SESSION['ip']) and ((strcmp($_SESSION['ip'], $_SERVER['REMOTE_ADDR']) !== false))){
    echo "Your IP doesn't match your session, your IP: ". $_SERVER['REMOTE_ADDR'] ." and your session IP: ". $_SESSION['ip']. ". <a href='/scripts/signout.php'>Click here to log out.</a>";
    die;
}
?>
于 2013-10-12T11:56:15.180 回答
1

如果字符串匹配, strcmp返回 0,并且在 php 中等于 false,你也不需要 md5 调用。

<?php
if (isset($_SESSION['ip']) && strcmp($_SESSION['ip'], $_SERVER['REMOTE_ADDR']) !== 0){
    echo "Your IP doesn't match your session, your IP: ". $_SERVER['REMOTE_ADDR'] ." and your session IP: ". $_SESSION['ip']. ". <a href='/scripts/signout.php'>Click here to log out.</a>";
    die;
}
//or you can just go 

if(isset($_SESSION['ip']) && $_SESSION['ip'] !== $_SERVER['REMOTE_ADDR']) {
/// stuff
}
于 2013-10-12T11:58:48.173 回答