当前位置:首页 » 编程语言 » phpip查询

phpip查询

发布时间: 2023-09-19 00:31:44

php中怎么获取访问当前页面电脑的IP地址

function getIP() {
$url=file_get_contents("";);
$ip=substr($url,85);
$ip=substr($ip,0,-4);
return $ip;
}
一个大神抛出的,不过需要访问一个链接(总是感觉慌慌的),建议用tp自带函数访问不到的再用这个方法

⑵ 用php如何获得用户ip地址


$ip = $_SERVER["REMOTE_ADDR"];

echo $ip;

//方法2:

$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] :
$_SERVER["REMOTE_ADDR"];

$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];

echo $user_IP;

//方法3:

function getRealIp()

{

$ip=false;

if(!empty($_SERVER["HTTP_CLIENT_IP"])){

$ip = $_SERVER["HTTP_CLIENT_IP"];

}

if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);

if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }

for ($i = 0; $i < count($ips); $i++) {

if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {

$ip = $ips[$i];

break;

}

}

}

return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);

}

echo getRealIp();

//方法4:

if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])

{

$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];

}

elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])

{

$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];

}

elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])

{

$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];

}

elseif (getenv("HTTP_X_FORWARDED_FOR"))

{

$ip = getenv("HTTP_X_FORWARDED_FOR");

}

elseif (getenv("HTTP_CLIENT_IP"))

{

$ip = getenv("HTTP_CLIENT_IP");

}

elseif (getenv("REMOTE_ADDR"))

{

$ip = getenv("REMOTE_ADDR");

}

else

{

$ip = "Unknown";

}

echo $ip ;

//方法5:

if(getenv('HTTP_CLIENT_IP')) {

$onlineip = getenv('HTTP_CLIENT_IP');

} elseif(getenv('HTTP_X_FORWARDED_FOR')) {

$onlineip = getenv('HTTP_X_FORWARDED_FOR');

} elseif(getenv('REMOTE_ADDR')) {

$onlineip = getenv('REMOTE_ADDR');

} else {

$onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];

}

echo $onlineip;

⑶ php用IP查询归属地

class ip_location
{
function init()
{
$this->wrydat = 'ip_area.dat';

$this->fp = fopen($this->wrydat, 'rb');
$this->getipnumber();
$this->getwryversion();

$this -> REDIRECT_MODE_0 = 0;
$this -> REDIRECT_MODE_1 = 1;
$this -> REDIRECT_MODE_2 = 2;
}

function get($str)
{
return $this->$str;
}

function set($str,$val)
{
$this->$str = $val;
}

function getbyte($length,$offset=null)
{
!is_null($offset) && fseek($this->fp, $offset, SEEK_SET);

return fread($this->fp, $length);
}

function packip($ip)
{
return pack('N', intval(ip2long($ip)));
}

function getlong($length=4, $offset=null)
{
$chr=null;
for($c=0;$length%4!=0&&$c<(4-$length%4);$c++)
{
$chr .= chr(0);
}
$var = unpack( 'Vlong', $this->getbyte($length, $offset).$chr);
return $var['long'];
}

function getwryversion()
{
$length = preg_match("/coral/i",$this->wrydat)?26:30;
$this->wrydat_version = $this->getbyte($length, $this->firstip-$length);
}

function getipnumber()
{
$this->firstip = $this->getlong();
$this->lastip = $this->getlong();
$this->ipnumber = ($this->lastip-$this->firstip)/7+1;
}

function getstring($data='', $offset=NULL)
{
$char = $this->getbyte(1,$offset);
while(ord($char) > 0)
{
$data .= $char;
$char = $this->getbyte(1);
}
return $data;
}

function iplocaltion($ip)
{
$ip = $this->packip($ip);
$low = 0;
$high = $this->ipnumber-1;
$ipposition = $this->lastip;
while($low <= $high)
{
$t = floor(($low+$high)/2);
if($ip < strrev($this->getbyte(4,$this->firstip+$t*7)))
$high = $t - 1;
else
{
if($ip > strrev($this->getbyte(4,$this->getlong(3))))
$low = $t + 1;
else
{
$ipposition = $this->firstip+$t*7;
break;
}
}
}
return $ipposition;
}

function getarea()
{
$b = $this->getbyte(1);
switch(ord($b))
{
case $this -> REDIRECT_MODE_0 :
return '';
break;
case $this -> REDIRECT_MODE_1:
case $this -> REDIRECT_MODE_2:
return $this->getstring('',$this->getlong(3));
break;
default:
return $this->getstring($b);
break;
}
}

function getiplocation($ip)
{
$ippos = $this->iplocaltion($ip);
$this->ip_range_begin = long2ip($this->getlong(4,$ippos));
$this->ip_range_end = long2ip($this->getlong(4,$this->getlong(3)));
$b = $this->getbyte(1);
switch(ord($b))
{
case $this -> REDIRECT_MODE_1:
$b = $this->getbyte(1,$this->getlong(3));
if(ord($b) == $this -> REDIRECT_MODE_2)
{
$countryoffset = $this->getlong(3);
$this->area = $this->getarea();
$this->country = $this->getstring('',$countryoffset);
}
else
{
$this->country = $this->getstring($b);
$this->area = $this->getarea();
}
break;
case $this -> REDIRECT_MODE_2:
$countryoffset = $this->getlong(3);
$this->area = $this->getarea();
$this->country = $this->getstring('',$countryoffset);
break;
default:
$this->country = $this->getstring($b);
$this->area = $this->getarea();
break;
}
}
}

---------------------------------------------------------------

调用方法:

$iploca = new ip_location;
$iploca -> init();
$iploca -> getiplocation($ip);

$area['country'] = str_replace(array('CZ88.NET'), '', $iploca -> get('country'));
$area['area'] = str_replace(array('CZ88.NET'), '', $iploca -> get('area'));

$area['country']=='' && $area['country']='未知';
$area['area']=='' && $area['area']='未知';
return $area;

⑷ php到底怎么获取IP地址,为什么三种方法获取的IP都不一样

你电脑接了路由器,127.0.0.1是本地回环地址,网上测试那个是公网IP地址,本机做服务器,服务器地址就是127.0.0.1

⑸ 利用php怎么获取用户ip

php获取用户真实ip的方法:
方法1:
//获取REMOTE_ADDR属性,直接可以得到ip
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
方法2:
//从Server中获取HTTP_VIA,如果获取到就用HTTP_X_FORWARDED_FOR的值替换,如果获取不到就取REMOTE_ADDR的值。
$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] :
$_SERVER["REMOTE_ADDR"];
$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
echo $user_IP;
方法3:
function getRealIp()
{
$ip=false; //初始化ip为false
if(!empty($_SERVER["HTTP_CLIENT_IP"])){ //如果HTTP_CLIENT_IP不为空
$ip = $_SERVER["HTTP_CLIENT_IP"]; //获取HTTP_CLIENT_IP的值
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //如果HTTP_X_FORWARDED_FOR不为空
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
//把HTTP_X_FORWARDED_FOR的值用,分割后存放数组ips
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }//遍历处理
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
$ip = $ips[$i]; //获得真实ip
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
echo getRealIp();

⑹ php 根据url 获取ip及端口

你可以试一下这个,比如你要想知道wqzbxh.top这个域名对应的IP 可以使用以下函数:

$hosts=gethostbynamel('wqzbxh.top'); //获取ip地址列表

print_r($hosts);

返回结是数组形式的返回结果:

⑺ php获取本机主板网卡IP地址.跳转读取相应的数据

大家一般都是用$_SERVER['REMOTE_ADDR']来获取用户IP,

但是如果使用了反向代理的,HTTP头中REMOTE_ADDR就不是用户的地址,反而是上一级代理的地址了。

经过我的研究有两种方法来获取用户的真实外网IP。

方法一:curl

复制代码代码如下:

function get_onlineip() {
$ch = curl_init('http://www.ip138.com/ip2city.asp');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch);
preg_match('/\[(.*)\]/', $a, $ip);
return $ip[1];
}

方法二:$_SERVER['HTTP_X_FORWARDED_FOR']来获取相应的地址

复制代码代码如下:

function get_onlineip() {
$onlineip = '';
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
return $onlineip;
}

⑻ 如何使用php获取本机IP地址

function ip() {
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$ip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
}
以上函数取自phpcms的获取ip方法

热点内容
怎样编程选股 发布:2025-02-02 08:22:02 浏览:415
电脑web应用服务器 发布:2025-02-02 08:05:31 浏览:811
电脑存储内存多少合适 发布:2025-02-02 08:00:15 浏览:110
登录界面android 发布:2025-02-02 07:53:23 浏览:842
编译时注解与运行时注解 发布:2025-02-02 07:53:14 浏览:818
怎样登陆ftp 发布:2025-02-02 07:44:44 浏览:637
疯狂点击脚本 发布:2025-02-02 07:38:10 浏览:73
pss算法 发布:2025-02-02 07:30:55 浏览:748
发信息脚本 发布:2025-02-02 07:03:07 浏览:741
l2l3缓存 发布:2025-02-02 06:56:47 浏览:524