检查会话IP是否与当前IP匹配 - PHP

时间:2013-10-12 11:51:45

标签: php session

所以我正在检查当前会话的会话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)

2 个答案:

答案 0 :(得分:1)

你正在比较会话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;
}
?>

答案 1 :(得分: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
}