PHP读取纯真IP数据库的函数

PHP读取纯真IP数据库的函数,以下代码提取自Discuz! X2

$tinyipfile Discuz! 独有高压缩比算法和快速检索的IP数据库路径
下载地址:http://www.discuz.net/thread-1873888-1-1.html
$fullipfile 纯真数据库路径
下载地址:http://update.cz88.net/soft/qqwry.rar

代码:

function convertip($ip) {
	$return = '';
	if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {

		$iparray = explode('.', $ip);

		if($iparray[0] == 10 || $iparray[0] == 127 || ($iparray[0] == 192 && $iparray[1] == 168) || ($iparray[0] == 172 && ($iparray[1] >= 16 && $iparray[1] <= 31))) {
			$return = '- LAN';
		} elseif($iparray[0] > 255 || $iparray[1] > 255 || $iparray[2] > 255 || $iparray[3] > 255) {
			$return = '- Invalid IP Address';
		} else {
			$tinyipfile = DISCUZ_ROOT.'./data/ipdata/tinyipdata.dat';
			$fullipfile = DISCUZ_ROOT.'./data/ipdata/wry.dat';
			if(@file_exists($tinyipfile)) {
				$return = convertip_tiny($ip, $tinyipfile);
			} elseif(@file_exists($fullipfile)) {
				$return = convertip_full($ip, $fullipfile);
			}
		}
	}

	return $return;

}

function convertip_tiny($ip, $ipdatafile) {

	static $fp = NULL, $offset = array(), $index = NULL;

	$ipdot = explode('.', $ip);
	$ip    = pack('N', ip2long($ip));

	$ipdot[0] = (int)$ipdot[0];
	$ipdot[1] = (int)$ipdot[1];

	if($fp === NULL && $fp = @fopen($ipdatafile, 'rb')) {
		$offset = @unpack('Nlen', @fread($fp, 4));
		$index  = @fread($fp, $offset['len'] - 4);
	} elseif($fp == FALSE) {
		return  '- Invalid IP data file';
	}

	$length = $offset['len'] - 1028;
	$start  = @unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);

	for ($start = $start['len'] * 8 + 1024; $start < $length; $start += 8) {

		if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip) {
			$index_offset = @unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
			$index_length = @unpack('Clen', $index{$start + 7});
			break;
		}
	}

	@fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
	if($index_length['len']) {
		return '- '.@fread($fp, $index_length['len']);
	} else {
		return '- Unknown';
	}

}

function convertip_full($ip, $ipdatafile) {

	if(!$fd = @fopen($ipdatafile, 'rb')) {
		return '- Invalid IP data file';
	}

	$ip = explode('.', $ip);
	$ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];

	if(!($DataBegin = fread($fd, 4)) || !($DataEnd = fread($fd, 4)) ) return;
	@$ipbegin = implode('', unpack('L', $DataBegin));
	if($ipbegin < 0) $ipbegin += pow(2, 32);
	@$ipend = implode('', unpack('L', $DataEnd));
	if($ipend < 0) $ipend += pow(2, 32);
	$ipAllNum = ($ipend - $ipbegin) / 7 + 1;

	$BeginNum = $ip2num = $ip1num = 0;
	$ipAddr1 = $ipAddr2 = '';
	$EndNum = $ipAllNum;

	while($ip1num > $ipNum || $ip2num < $ipNum) {
		$Middle= intval(($EndNum + $BeginNum) / 2);

		fseek($fd, $ipbegin + 7 * $Middle);
		$ipData1 = fread($fd, 4);
		if(strlen($ipData1) < 4) {
			fclose($fd);
			return '- System Error';
		}
		$ip1num = implode('', unpack('L', $ipData1));
		if($ip1num < 0) $ip1num += pow(2, 32);

		if($ip1num > $ipNum) {
			$EndNum = $Middle;
			continue;
		}

		$DataSeek = fread($fd, 3);
		if(strlen($DataSeek) < 3) {
			fclose($fd);
			return '- System Error';
		}
		$DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
		fseek($fd, $DataSeek);
		$ipData2 = fread($fd, 4);
		if(strlen($ipData2) < 4) {
			fclose($fd);
			return '- System Error';
		}
		$ip2num = implode('', unpack('L', $ipData2));
		if($ip2num < 0) $ip2num += pow(2, 32);

		if($ip2num < $ipNum) {
			if($Middle == $BeginNum) {
				fclose($fd);
				return '- Unknown';
			}
			$BeginNum = $Middle;
		}
	}

	$ipFlag = fread($fd, 1);
	if($ipFlag == chr(1)) {
		$ipSeek = fread($fd, 3);
		if(strlen($ipSeek) < 3) {
			fclose($fd);
			return '- System Error';
		}
		$ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
		fseek($fd, $ipSeek);
		$ipFlag = fread($fd, 1);
	}

	if($ipFlag == chr(2)) {
		$AddrSeek = fread($fd, 3);
		if(strlen($AddrSeek) < 3) {
			fclose($fd);
			return '- System Error';
		}
		$ipFlag = fread($fd, 1);
		if($ipFlag == chr(2)) {
			$AddrSeek2 = fread($fd, 3);
			if(strlen($AddrSeek2) < 3) {
				fclose($fd);
				return '- System Error';
			}
			$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
			fseek($fd, $AddrSeek2);
		} else {
			fseek($fd, -1, SEEK_CUR);
		}

		while(($char = fread($fd, 1)) != chr(0))
		$ipAddr2 .= $char;

		$AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
		fseek($fd, $AddrSeek);

		while(($char = fread($fd, 1)) != chr(0))
		$ipAddr1 .= $char;
	} else {
		fseek($fd, -1, SEEK_CUR);
		while(($char = fread($fd, 1)) != chr(0))
		$ipAddr1 .= $char;

		$ipFlag = fread($fd, 1);
		if($ipFlag == chr(2)) {
			$AddrSeek2 = fread($fd, 3);
			if(strlen($AddrSeek2) < 3) {
				fclose($fd);
				return '- System Error';
			}
			$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
			fseek($fd, $AddrSeek2);
		} else {
			fseek($fd, -1, SEEK_CUR);
		}
		while(($char = fread($fd, 1)) != chr(0))
		$ipAddr2 .= $char;
	}
	fclose($fd);

	if(preg_match('/http/i', $ipAddr2)) {
		$ipAddr2 = '';
	}
	$ipaddr = "$ipAddr1 $ipAddr2";
	$ipaddr = preg_replace('/CZ88\.NET/is', '', $ipaddr);
	$ipaddr = preg_replace('/^\s*/is', '', $ipaddr);
	$ipaddr = preg_replace('/\s*$/is', '', $ipaddr);
	if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
		$ipaddr = '- Unknown';
	}

	return '- '.$ipaddr;

}

php gethostbyname根据域名获取ip地址函数

gethostbyname() 根据域名获取ip地址函数

语法:

string gethostbyname ( string $hostname )

参数:

hostname:要查询的域名

返回:
本函数可返回某个机器名称 (Domain Name) 的 IP 网址 (IP Address)。若执行失败,则返回原来的机器名称。

使用范例:
下例的返回值为 49.247.252.201

$ip = gethostbyname('www.hujuntao.com');
echo $ip;

PHP Alexa Rank类

php类获取Alexa排名,获取以下相关信息:

  1. 人气排名
  2. 日访问量
  3. 反向链接
  4. 响应速度

PHP4版本代码

/* the alexa rank class */
class alexa
{
	/* initial vars */
	var $xml;
	var $values;
	var $alexa_address;

	/* the constructor */
	function alexa($alexa_address,$domain)
	{
		$this->alexa_address = $alexa_address;
		$this->xml = $this->get_data($domain);
		$this->set();
	}

	/* gets the xml data from Alexa */
	function get_data($domain)
	{
		$url = $this->alexa_address.'http://'.$domain;
		$xml = file_get_contents($url);
		return $xml;
	}

	/* set values in the XML that we want */
	function set()
	{
		$this->values['rank'] = (preg_match('/POPULARITY URL="[a-z0-9\\-\\.\\/]{1,}" TEXT="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);
		$this->values['reach'] = (preg_match('/REACH RANK="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);
		$this->values['linksin'] = (preg_match('/LINKSIN NUM="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);
	}

	/* returns the requested value */
	function get($value)
	{
		return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.');
	}
}

PHP5版本代码

/* the alexa rank class */
class alexa
{
	/* initial vars */
	var $xml;
	var $values;
	var $alexa_address;

	/* the constructor */
	function alexa($alexa_address,$domain)
	{
		$this->alexa_address = $alexa_address;
		$this->xml = $this->get_data($domain);
		$this->set();
	}

	/* gets the xml data from Alexa */
	function get_data($domain)
	{
		$url = $this->alexa_address.'http://'.$domain;
		$xml = simplexml_load_file($url) or die('Cannot retrieve feed');
		return $xml;
	}

	/* set values in the XML that we want */
	function set()
	{
		$this->values['rank'] = ($this->xml->SD->POPULARITY['TEXT'] ? number_format($this->xml->SD->POPULARITY['TEXT']) : 0);
		$this->values['reach'] = ($this->xml->SD->REACH['RANK'] ? number_format($this->xml->SD->REACH['RANK']) : 0);
		$this->values['linksin'] = ($this->xml->SD->LINKSIN['NUM'] ? number_format($this->xml->SD->LINKSIN['NUM']) : 0);
	}

	/* returns the requested value */
	function get($value)
	{
		return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.');
	}
}

使用cURL
如果您使用curl库,你可以简单地修改get_data()函数:

/* gets the XML data from Alexa */
function get_data($domain)
{
	$url = $this->alexa_address.'http://'.$domain;
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	$xml = curl_exec($ch);
	curl_close($ch);
	return $xml;
}

用法:

/* retrieve & display rank */
$alexa_connector = new alexa('http://alexa.com/xml/dad?url=','digg.com'); // domain only!
echo 'Rank :: '.$alexa_connector->get('rank'); // returns 118
echo '';
echo 'Reach :: '.$alexa_connector->get('reach'); // returns 95
echo '';
echo 'Links In :: '.$alexa_connector->get('linksin'); // returns 34,414

php获取Google PageRank值代码

php获取google PageRank值代码,注意最新的Google PageRank API地址以变为“http://toolbarqueries.google.com/tbr?client=navclient-auto&features=Rank:&q=info:”

class PageRank {
	function __construct($url) {
		$url = 'info:' . $url;
		$hash = '6' . $this->c($this->e($this->b($url)));
		$fetch = 'http://toolbarqueries.google.com/tbr?client=navclient-auto&ch=' . $hash . '&ie=UTF-8&oe=UTF-8&features=Rank&q=' . $url;
		if(function_exists('curl_init')) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $fetch);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$out = curl_exec($ch);
			curl_close($ch);
		} else {
			$out = file_get_contents($fetch);
		}
		$out = trim($out);
		if(strlen($out) > 0) {
			$this->pr = substr($out, 9);
		} else {
			$this->pr = -1;
		}
	}

	function b($hash) {
		$j = array();
		$length = strlen($hash);
        for($i = 0; $i < $length; $i++) {
        	$j[$i] = ord($hash[$i]);
        }
        return $j;
    }
	function c($l) {
		$l = ((($l / 7) << 2) | (($this->h($l, 13)) & 7));
		$j = array();
		$j[0] = $l;
		for($i = 1; $i < 20; $i++) {
			$j[$i] = $j[$i - 1] - 9;
		}
		$l = $this->e($this->g($j), 80);
		return $l;
	}
	function e($hash) {
		$r = 3862272608;
        $j = count($hash);
        $p = 2654435769;
        $o = 2654435769;
        $n = 3862272608;
        $l = 0;
        $m = $j;
        $q = array();
        while ($m >= 12) {
            $p += ($hash[$l] + ( $hash[$l + 1] < < 8 ) + ($hash[$l + 2] << 16) + ($hash[$l + 3] << 24));
            $o += ($hash[$l + 4] + ( $hash[$l + 5] << 8 ) + ($hash[$l + 6] << 16) + ($hash[$l + 7] << 24));
            $n += ($hash[$l + 8] + ( $hash[$l + 9] << 8 ) + ($hash[$l + 10] << 16) + ($hash[$l + 11] << 24));
            $q = $this->s($p, $o, $n);
            $p = $q[0];
            $o = $q[1];
            $n = $q[2];
            $l += 12;
            $m -= 12;
        }
        $n += $j;
        switch ($m) {
        case 11:
            $n += $hash[$l + 10] < < 24;
        case 10:
            $n += $hash[$l + 9] << 16;
        case 9:
            $n += $hash[$l + 8] << 8;
        case 8:
            $o += $hash[$l + 7] << 24;
        case 7:
            $o += $hash[$l + 6] << 16;
        case 6:
            $o += $hash[$l + 5] << 8;
        case 5:
            $o += $hash[$l + 4];
        case 4:
            $p += $hash[$l + 3] << 24;
        case 3:
            $p += $hash[$l + 2] << 16;
        case 2:
            $p += $hash[$l + 1] << 8;
        case 1:
            $p += $hash[$l];
        }
        $q = $this->s($p, $o, $n);
        return ($q[2] < 0) ? (4294967296 + $q[2]) : $q[2];
	}
	function f($j, $i) {
        $k = 2147483648;
        if ($k & $j) {
            $j = $j >> 1;
            $j &= ~$k;
            $j |= 1073741824;
            $j = $j >> ($i - 1);
        } else {
            $j = $j >> $i;
        }
        return $j;
    }

    function g($j) {
    	$l = array();
    	$length = count($j);
    	for($k = 0; $k < $length; $k++) {
    	    for ($m = $k * 4; $m <= $k * 4 + 3; $m++) {
                $l[$m] = $j[$k] & 255;
                $j[$k] = $this->f($j[$k], 8);
            }
    	}
        return $l;
    }

    function h($j, $l) {
        $k = floor($j / $l);
        return ($j - $k * $l);
    }
	function s($t, $k, $u) {
		$t -= $k;
		$t -= $u;
		$t ^= ($this->f($u, 13));
		$k -= $u;
		$k -= $t;
		$k ^= ($t < < 8);
		$u -= $t;
		$u -= $k;
		$u ^= ($this->f($k, 13));
		$t -= $k;
		$t -= $u;
		$t ^= ($this->f($u, 12));
		$k -= $u;
		$k -= $t;
		$k ^= ($t < < 16);
		$u -= $t;
		$u -= $k;
		$u ^= ($this->f($k, 5));
		$t -= $k;
		$t -= $u;
		$t ^= ($this->f($u, 3));
		$k -= $u;
		$k -= $t;
		$k ^= ($t < < 10);
		$u -= $t;
		$u -= $k;
		$u ^= ($this->f($k, 15));
		return array($t, $k, $u);
	}
}

$pr = new PageRank('http://www.hujuntao.com');
echo $pr->pr;

最核心的地方就是这个checknum算法,有了这段代码你也可以建一个PR值查询工具。

本站PR值查询地址:http://www.hujuntao.com/api/pr/

用PHP伪造referer地址

很多时候我们都会用到referer地址,通过判断上一页是从哪里来的,我们可以了解很多信息,但是现在referer并不是那么可靠的数据了,因为我们可以伪造referer地址。这里分别介绍CURL、SOCKET、file_get_contents实现方法,详细代码如下:

CURL方式

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "http://www.hujuntao.com");
curl_setopt ($ch, CURLOPT_REFERER, "http://www.hujuntao.com/");
curl_exec ($ch);
curl_close ($ch);

SOCKET方式

$server = 'www.hujuntao.com';
$host = 'www.hujuntao.com';
$target = 'index.php';
$referer = 'http://www.hujuntao.com/'; // Referer
$port = 80;
$fp = fsockopen($server, $port, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)\n";
}
else
{
$out = "GET $target HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Referer: $referer\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp))
{
echo fgets($fp, 128);
}
fclose($fp);
}

file_get_contents方法

$opt=array('http'=>array('header'=>"Referer: $refer")); 
$context=stream_context_create($opt); 
$file_contents = file_get_contents($url,false, $context);

通过上面的代码,我们就把referer地址伪装为http://www.hujuntao.com,你可以写一段代码:
$_SERVER[‘HTTP_REFERER’];
查看到这个referer地址,就是这么简单,所以referer也不是什么可靠的数据了。