用IP138解析来访IP

2011年2月18日 | 分类: Document | 标签: , ,

一个使用IP136网站来解析IP物理地址的PHP函数

<?php
 function GetIP() { //获取IP
    if ($_SERVER["HTTP_X_FORWARDED_FOR"])
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    else if ($_SERVER["HTTP_CLIENT_IP"])
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    else if ($_SERVER["REMOTE_ADDR"])
        $ip = $_SERVER["REMOTE_ADDR"];
    else if (getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else
        $ip = "Unknown";
    return $ip;
}
$myip=GetIP();
$url = "http://www.ip138.com/ips.asp?ip=".GetIP()."&action=2";
$content = file_get_contents($url,"gb2312");
$sourceStr = $content;
$startStr = "<ul class=\"ul1\"><li>";
$endStart = "</li>";
function getContent ( $sourceStr, $startStr, $endStart )   
{   
$s = preg_quote( urldecode( $startStr ) );   
$e = preg_quote( urldecode( $endStart ) );   
$s = str_replace( " ", "[[:space:]]", $s );   
$e = str_replace( " ", "[[:space:]]", $e );   
$s = str_replace( "\r\n", "[[:cntrl:]]", $s );   
$e = str_replace( "\r\n", "[[:cntrl:]]", $e );    
preg_match_all( "@" . $s . "(.*?)". $e ."@is", $sourceStr, $tpl );    
$content = $tpl[1];   
$content = implode( "", $content );   
return $content;   
}
$myip=mb_convert_encoding(getContent($content,$startStr,$endStart),'UTF-8', 'GB2312');
echo str_replace("本站主数据:","",$myip);
?>
目前还没有任何评论.

返回顶部