PHP函数拒绝将IP地址添加到MYSQL中

时间:2016-08-27 15:17:14

标签: php mysql

现在已经使用这个脚本很长一段时间了,但无论我做什么,都不会将IP地址添加到数据库中。它将它添加为NULL值,我无法弄清楚为什么。我不想使用IP2Long或long2ip,因为IP地址需要匹配传入连接甚至登录到站点。我用来获取IP的代码是下面的。

if (!empty($_SERVER['HTTP_CLIENT_IP'])){
      $IP = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
        $IP = $_SERVER['REMOTE_ADDR'];
}

哪个效果很好。只在回显中查看它时,它会显示我想要的正确IP地址,因此代码不是问题。下一个功能是它在注册时如何将代码插入数据库。

function Register()
{
    $con = mysqli_connect("removed","removed","removed","removed");
    if(!empty($_POST['Key']) AND !empty($_POST['Username']))
    {   
        $errors="";
        $query="SELECT `Key` FROM `Keys` 
                WHERE `Key`= '".mysqli_real_escape_string($con,$_POST['Key'])."'";
        $result = mysqli_query($con,$query);
        $row = mysqli_fetch_array($result);
        if($row['Key'])
        {
            $query="SELECT `Activated` FROM `Keys` WHERE `Key`= '".mysqli_real_escape_string($con,$_POST['Key'])."'";
            $result = mysqli_query($con,$query);
            $row = mysqli_fetch_array($result);
            if($row['Activated']){
                $errors= "This key has already been activated. You cannot create an account with this key.";
            }
        }
        else{
            $errors= "Key not found. Please contact us for assistance!";   
        }
        if($errors){
            echo $errors;
        }else{
            //ip insert update
            $query="UPDATE `Keys` 
                      SET `IP` = '$IP', 
                      `Activated` = '1', `Username` = '".mysqli_real_escape_string($con,$_POST['Username'])."' 
                     WHERE `Key`= '".mysqli_real_escape_string($con,$_POST['Key'])."'";

            $result = mysqli_query($con,$query);

            if($result){
                $query="UPDATE `Keys` SET `Timestamp` = NOW(), `IP` = '$IP', `DATE` = NOW() WHERE `Keys`= '".mysqli_real_escape_string($con,$_POST['Key'])."'";
                $result=mysqli_query($con,$query);
            }else{
                $errors.="Could not sign up. Please contact us for assistance.";
            }
            //email check 
            $query = "SELECT*FROM `Users` WHERE email='".mysqli_real_escape_string($con,$_POST['Email'])."'";
            $result = mysqli_query($con,$query);
            $results = mysqli_num_rows($result);
            if($results>0){
                $errors.="The email provided is already registered. </br>";
            }
        //username check
            $query = "SELECT*FROM `Users` WHERE username='".mysqli_real_escape_string($con,$_POST['Username'])."'";
            $result = mysqli_query($con,$query);
            $results = mysqli_num_rows($result);
            if($results>0){
                $errors.= "The username provided is already registered. </nr>";
            }

            if($errors){
              echo $errors;
            }else{
                $query = "INSERT INTO `Users` (`username`,`name`,`email`, `password`) VALUES ('".mysqli_real_escape_string($con, $_POST['Username'])."','".mysqli_real_escape_string($con, $_POST['Name'])."','".mysqli_real_escape_string($con, $_POST['Email'])."', '".md5(md5($_POST['Email']).$_POST['password'])."')";
                $result = mysqli_query($con,$query);
                if($result){
                    echo '<script type="text/javascript"> window.onload = function () { alert("Registration and activation was successful."); } </script>';

                }else{
                    echo '<script type="text/javascript"> window.onload = function () { alert("An error occurred. Please contact us for assistance."); } </script>';
                }
            }
        }
    }
}

此代码不会将IP地址添加到系统中,我无法弄清楚原因。它在其他网站上运行良好,而不是这个。

有什么想法吗?

除了客户的系统之外不会是一个问题,我希望它完美无缺。感谢

0 个答案:

没有答案