简体   繁体   English

无法在php中访问访客的IP地址

[英]Cannot access visitor's ip address in php

i am trying to store ip addresses of people who are visiting my web site. 我正在尝试存储正在访问我的网站的人的IP地址。 For that i use the below given code. 为此,我使用下面给出的代码。

$serverIP=$_SERVER['REMOTE_ADDR'];

but instead of getting an IP like 112.200.xxx.xxx (when i visit), i got 192.9.200.195 .. 但是我没有得到像112.200.xxx.xxx这样的IP(当我访问时),而是获得了192.9.200.195 ..

somebody please help me 有人请帮助我

thanks in advance 提前致谢

tismon tismon

try this, maybe its a proxy(?) 试试这个,也许它是一个代理(?)

if ($_SERVER['HTTP_X_FORWARDED_FOR'])
{
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
  $ip = $_SERVER['REMOTE_ADDR'];
} 
echo $ip;

looks like you're thinking 192.9.200.195 is a local ip-adress - but its not, local adresses you mean are starting with 192.168. 看起来您在想192.9.200.195是本地ip地址-但不是,您的本地地址以192.168.开头 . 192.9.200.195 looks ok to me, if it's not, please try to explain you problem more detailed. 192.9.200.195对我来说还可以,如果不是,请尝试向您详细解释问题。

try 尝试

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
echo getRealIpAddr();

You can also try this: 您也可以尝试以下操作:

<?php
$var = file_get_contents('http://www.whatismyip.com/automation/n09230945.asp');
print $var;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM