捕获用户机器的IP地址

时间:2014-03-14 07:39:34

标签: php ip

<?php
if (isset($_GET['action']) && ($_GET['action'] == 'submit')) {
    $name    = $_POST['name'];
    $message = $_POST['message'];
    $email   = $_POST['email'];

    $ip        = $_SERVER["REMOTE_ADDR"];
    $messageip = "User IP: $ip\n\n" . $messageip;

    $sql   = "SELECT * FROM tbl_user WHERE email = '" . $email . "'";
    $query = mysql_query($sql);
    $count = mysql_num_rows($query);
    if ($count >= 1) {

        echo "User Already in Exists<br/>";
    } else {

        $newUser = "INSERT INTO tbl_user(name,message,email,ip_address) values('$name','$message','$email','$messageip')";
        $query2  = mysql_query($newUser);

        if ($query2) {
            echo "You are now registered<br/>";
        } else {
            echo "Error adding user in database<br/>";
        }
    }
    echo $name . '<br/>';
    echo $message . '<br/>';
    echo $email . '<br/>';
    echo $messageip . '<br/>';
}
?>

我正在使用此代码来捕获IP地址,但我无法获取特定用户的机器IP,我可以动态地显示该地址,所以有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您似乎正在连接一个错误的(空)var。

尝试更改此内容:

  

$ messageip =&#34;用户IP:$ ip \ n \ n&#34; 。 $ messageip;

到此:

  

$ messageip =&#34;用户IP:$ ip \ n \ n&#34; 。 $ IP;