发布IP'时出现$ _SERVER [" REMOTE_ADDR"]问题

时间:2017-09-21 01:22:35

标签: php mysql

经过大量测试后,我终于能够$_SERVER["REMOTE_ADDR"]成功将IP发布到我的MySQL数据库中

除了它实际上并非如此 发布正确的IP。它只收集前4个数字,并且不会在数字组之后放置句号。(我尝试在用户注册帐户时记录IP)

可能是什么问题,我该如何解决?

以下是我的当前代码:

<?php 
session_start();

// variable declaration
$username = "";
$email    = "";
$errors = array(); 
$_SESSION['success'] = "";
$ip = $_SERVER["REMOTE_ADDR"];


// connect to database
$db = mysqli_connect('host','user','pass','db')
 or die('Error connecting to MySQL server.');

// REGISTER USER
if (isset($_POST['reg_user'])) {
    // receive all input values from the form
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
    $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
    $ip = mysqli_real_escape_string($db, $_POST = $_SERVER["REMOTE_ADDR"]);

    // form validation: ensure that the form is correctly filled
    if (empty($username)) { array_push($errors, "Username is required"); }
    if (empty($email)) { array_push($errors, "Email is required"); }
    if (empty($password_1)) { array_push($errors, "Password is required"); }

    if ($password_1 != $password_2) {
        array_push($errors, "The two passwords do not match");
    }

    // register user if there are no errors in the form
    if (count($errors) == 0) {
        $password = md5($password_1);//encrypt the password before saving in the database
        $query = "INSERT INTO users (username, email, password, ip)
                  VALUES('$username', '$email', '$password', '$ip')";
        mysqli_query($db, $query);
        $_SESSION['username'] = $username;
        $_SESSION['success'] = "You are now logged in";
        header('location: index.php');

        mysql_error();
    }

}

// ... 

// LOGIN USER
if (isset($_POST['login_user'])) {
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);

    if (empty($username)) {
        array_push($errors, "Username is required");
    }
    if (empty($password)) {
        array_push($errors, "Password is required");
    }

    if (count($errors) == 0) {
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $results = mysqli_query($db, $query);

        if (mysqli_num_rows($results) == 1) {
            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('location: index.php');
        }else {
            array_push($errors, "Wrong username/password combination");
        }
    }
}
?>

与$ _SERVER相关的代码[&#34; REMOTE_ADDR&#34;];

$ip = $_SERVER["REMOTE_ADDR"];

$ip = mysqli_real_escape_string($db, $_POST = $_SERVER["REMOTE_ADDR"]);

        $query = "INSERT INTO users (username, email, password, ip)
                  VALUES('$username', '$email', '$password', '$ip')";

1 个答案:

答案 0 :(得分:4)

您需要确保数据库表列IP是(var)char,至少包含15个字符,例如VARCHAR(15)。 (或ipv6超过15)

另外,为什么要写$_POST = $_SERVER["REMOTE_ADDR"],从而尝试覆盖$ _POST?将其更改为$_SERVER["REMOTE_ADDR"]

最后,查找参数化查询。