如何在php中存储数据库中访问者的IP地址?

时间:2015-01-28 06:30:54

标签: php html mysql

我试过这个php方法来获取ip地址,但它提供了错误的输出。 常规方法

 <?php     echo $_SERVER['REMOTE_ADDR'];   ?>

未提供适当的输出。当我使用这种方法时,我得到像

这样的输出
  

IP地址:&#39; :: 1&#39;

我的剧本有什么问题吗? 还提供了有关在数据库中存储这些IP地址的详细信息 提前致谢

4 个答案:

答案 0 :(得分:6)

':: 1'表示它是您的本地主机IP地址。

:: 1 == 127.0.0.1

你也可以尝试这个。

$ip=$_SERVER['REMOTE_ADDR'];
echo "IP address= $ip";

如果您的应用程序托管在您尝试请求的同一台计算机上,它将始终返回“:: 1”,这意味着LocalHost。否则它将返回客户端IP地址。

答案 1 :(得分:4)

完整的一个:D:p(不幸的是,这个脚本在mysql中而不是mysqli / pdo。你可以稍后修改它。

<强> File1.php

<?php
//connect to database
        class usersOnline {

            var $timeout = 300;
            var $count = 0;
            var $error;
            var $i = 0;

            function usersOnline () {
                $this->timestamp = time();
                $this->ip = $this->ipCheck();
                $this->new_user();
                $this->delete_user();
                $this->count_users();
                }

            function ipCheck() {
                if (getenv('HTTP_CLIENT_IP')) {
                    $ip = getenv('HTTP_CLIENT_IP');
                    }
                elseif (getenv('HTTP_X_FORWARDED_FOR')) {
                    $ip = getenv('HTTP_X_FORWARDED_FOR');
                    }
                elseif (getenv('HTTP_X_FORWARDED')) {
                    $ip = getenv('HTTP_X_FORWARDED');
                    }
                elseif (getenv('HTTP_FORWARDED_FOR')) {
                    $ip = getenv('HTTP_FORWARDED_FOR');
                    }
                elseif (getenv('HTTP_FORWARDED')) {
                    $ip = getenv('HTTP_FORWARDED');
                    }
                else {
                    $ip = $_SERVER['REMOTE_ADDR'];
                    }
                return $ip;
                }
            function new_user() {
                $cekIP = "SELECT ip FROM useronline WHERE ip='$this->ip'";
                $resultCekIp=mysql_query($cekIP);
                $countCekIp=mysql_num_rows($resultCekIp);
                if($countCekIp>0){
                    $insert1 = mysql_query ("UPDATE useronline SET timestamp='$this->timestamp', date=NOW(), ip='', distinct_ip='$this->ip'");
                    if (!$insert1) {
                        $this->error[$this->i] = "Unable to record new visitor\r\n";            
                        $this->i ++;
                        }
                    }
                else{
                    $insert2 = mysql_query ("INSERT INTO useronline (timestamp, date, ip, distinct_ip) VALUES ('$this->timestamp',NOW(), '$this->ip', '$this->ip')");
                    if (!$insert2) {
                        $this->error[$this->i] = "Unable to record new visitor\r\n";            
                        $this->i ++;
                        }
                    }
                }
            function delete_user() {
                $delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
                if (!$delete) {
                    $this->error[$this->i] = "Unable to delete visitors";
                    $this->i ++;
                    }
                }
            function count_users() {
                if (count($this->error) == 0) {
                    $count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));
                    return $count;
                    }
                }
            }
        ?>

<强> File2.php

<?php
include('file1.php');
$jml_ol = new usersOnline();
if (count($jml_ol->error) == 0) {
    if ($jml_ol->count_users() == 1) {
        echo $jml_ol->count_users() . "<br />";
        echo "Your IP: " . $jml_ol->ipCheck();
        }
    else{
        echo $jml_ol->count_users() . "<br />";
        echo "Your IP: " . $jml_ol->ipCheck();
        }
}
?> 
身体或div中的

<body>
<?php include('File2.php'); ?>
</body>

答案 2 :(得分:2)

以下是一些示例代码

 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'];
}

here

领取

答案 3 :(得分:1)

您可以使用返回int的ip2long。检查-1FALSE的返回值,以确保返回有效的IP。

您看到的输出是IPv6中的本地主机。