當前位置:首頁 » 編程語言 » 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方法

熱點內容
android獲取應用版本 發布:2025-02-02 05:54:19 瀏覽:744
pythonif比較 發布:2025-02-02 05:24:03 瀏覽:259
已連接的無線網如何知道密碼 發布:2025-02-02 04:53:51 瀏覽:634
android編程入門經典pdf 發布:2025-02-02 04:46:19 瀏覽:57
安卓什麼軟體測試手機電池 發布:2025-02-02 04:28:52 瀏覽:996
手機上傳快 發布:2025-02-02 04:27:46 瀏覽:308
電腦配置詳解圖解都有哪些 發布:2025-02-02 04:26:27 瀏覽:716
景區應該有什麼配置 發布:2025-02-02 04:09:08 瀏覽:120
c語言與java工作 發布:2025-02-02 03:59:57 瀏覽:283
qq買什麼不要支付密碼 發布:2025-02-02 03:50:29 瀏覽:496