當前位置:首頁 » 編程語言 » qqwryphp

qqwryphp

發布時間: 2022-07-15 16:46:25

Ⅰ qqwry.dat這個文件用php怎麼讀取

$fp = fopen('qqwry.dat','rb');
while(!feof($fp)){
echo fgets($fp,999)."<br />";
}

Ⅱ 一段基於QQWRY.DAT通過AJAX+PHP實現的代碼在我的APACHE上跑會報錯,希望高手幫忙看看~

APMServ5.2.6相當好,不過對於新手來說可能不夠簡易。你的問題應該是沒有讀寫許可權,APMServ5.2.6在設置虛擬域時默認打開安全模式,並僅給你指定的目錄帶讀寫許可權,也就算說你的程序在默認的設置下不能讀寫其他目錄的文件。所以那是你的設置問題,如果不行你最好用phpinfo查下你的環境。

Ⅲ QQWry.Dat的工作原理

一個利用 QQWry.Dat 實現 IP 地址高效檢索(PHP),當時是直接從文件里查詢的,雖然在創建類的一個實例後,可以進行多次查詢,不用每次查詢都打開文件,已經很快了,但是我想也許還可以更快點兒,於是我想到了用共享內存。下面這個程序就是我用共享內存來實現的快速查詢類,它在第一個實例被創建時將 QQWry.Dat 文件讀入共享內存,以後每次查詢都直接從共享內存中查找數據,而不需要再讀文件了,現在的查詢效率是原來的 1.5 倍。不過使用這個類要注意一點,這個類是一個 Singleton 類,所以需要用 & IpLocation::getInstance 來返回此類的實例引用,而不要用 new IpLocation 來創建實例,不然就不能保證實例的唯一性了。如果在一個頁面內創建多個 IpLocation 實例的話,你會得到內存錯誤,嚴重情況下可能會使你的伺服器崩潰,因此我才把它定義為一個 Singleton 類。另外這個類的實例被創建一次後,文件內容就被讀入到共享內存中了,因此如果伺服器不重新啟動,內存中的 QQWry.Dat 的文件數據就不會更新。
因為用了共享內存,因此對系統有一定的要求,如果系統是 Windows,系統需要 Windows2000 以上系統,PHP 作為 IIS 的 ISAPI 運行才支持共享內存,或者是 Linux 下 PHP 作為 Apache 模塊運行,CGI 和 CLI 方式下不可以。

Ⅳ php中用「qqwry.dat文件」獲取ip,請問這個文件可以用絕對路徑調用嗎

只要放到伺服器上就能用絕對路徑
比如你的伺服器地址是192.168.1.2
那就用 192.168.1.2/你的網站名/各級目錄/qqwry.dat

這樣的方法調用
遠程的缺點就是伺服器掛了或者網不通其他的就全都用不了了 優點就是方便

Ⅳ php怎麼和qqwry.dat連接判斷

r
<?php  
define('__QQWRY__' , dirname(__FILE__)."/QQWry.Dat");
//echo __QQWRY__;
class QQWry{
var $StartIP=0;
var $EndIP=0;
var $Country=''
var $Local=''

var $CountryFlag=0; // 標識 Country位置
// 0x01,隨後3位元組為Country偏移,沒有Local
// 0x02,隨後3位元組為Country偏移,接著是Local
// 其他,Country,Local,Local有類似的壓縮。可能多重引用。
var $fp;

var $FirstStartIp=0;
var $LastStartIp=0;
var $EndIpOff=0 ;

function getStartIp($RecNo){
$offset=$this->FirstStartIp+$RecNo * 7 ;
@fseek($this->fp,$offset,SEEK_SET) ;
$buf=fread($this->fp ,7) ;
$this->EndIpOff=ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])* 256*256);
$this->StartIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
return $this->StartIp;
}

function getEndIp(){
@fseek ( $this->fp , $this->EndIpOff , SEEK_SET ) ;
$buf=fread ( $this->fp , 5 ) ;
$this->EndIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
$this->CountryFlag=ord ( $buf[4] ) ;
return $this->EndIp ;
}

function getCountry(){
switch ( $this->CountryFlag ) {
case 1:
case 2:
$this->Country=$this->getFlagStr ( $this->EndIpOff+4) ;
//echo sprintf('EndIpOffset=(%x)',$this->EndIpOff );
$this->Local=( 1 == $this->CountryFlag )? '' : $this->getFlagStr ( $this->EndIpOff+8);
break ;
default :
$this->Country=$this->getFlagStr ($this->EndIpOff+4) ;
$this->Local=$this->getFlagStr ( ftell ( $this->fp )) ;
}
}


function getFlagStr ($offset){
$flag=0 ;
while(1){
@fseek($this->fp ,$offset,SEEK_SET) ;
$flag=ord(fgetc($this->fp ) ) ;
if ( $flag == 1 || $flag == 2 ) {
$buf=fread ($this->fp , 3 ) ;
if ($flag==2){
$this->CountryFlag=2;
$this->EndIpOff=$offset - 4 ;
}
$offset=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])* 256*256);
}
else{
break ;
}

}
if($offset<12)
return ''
@fseek($this->fp , $offset , SEEK_SET ) ;

return $this->getStr();
}

function getStr ( )
{
$str='' ;
while ( 1 ) {
$c=fgetc ( $this->fp ) ;
//echo "$cn" ;

if(ord($c[0])== 0 )
break ;
$str.= $c ;
}
//echo "$str n";
return $str ;
}


function qqwry ($dotip='') {
if(!$dotip)return;
if(ereg("^(127)",$dotip)){$this->Country=本地網路;return;}
elseif(ereg("^(192)",$dotip)) {$this->Country=區域網;return;}

$nRet;
$ip=$this->IpToInt ( $dotip );
$this->fp= fopen(__QQWRY__, "rb");
if ($this->fp == NULL) {
$szLocal= "OpenFileError";
return 1;

}
@fseek ( $this->fp , 0 , SEEK_SET ) ;
$buf=fread ( $this->fp , 8 ) ;
$this->FirstStartIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
$this->LastStartIp=ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])*256*256) + (ord($buf[7])*256*256*256);

$RecordCount= floor( ( $this->LastStartIp - $this->FirstStartIp ) / 7);
if ($RecordCount <= 1){
$this->Country="FileDataError";
fclose($this->fp) ;
return 2 ;
}

$RangB= 0;
$RangE= $RecordCount;
// Match ...
while ($RangB < $RangE-1)
{
$RecNo= floor(($RangB + $RangE) / 2);
$this->getStartIp ( $RecNo ) ;

if ( $ip == $this->StartIp )
{
$RangB=$RecNo ;
break ;
}
if ($ip>$this->StartIp)
$RangB= $RecNo;
else
$RangE= $RecNo;
}
$this->getStartIp ( $RangB ) ;
$this->getEndIp ( ) ;

if ( ( $this->StartIp <= $ip ) && ( $this->EndIp >= $ip ) ){
$nRet=0 ;
$this->getCountry ( ) ;
//這樣不太好..............所以..........
$this->Local=str_replace("(我們一定要解放台灣!!!)", "", $this->Local);
}
else{
$nRet=3 ;
$this->Country='未知' ;
$this->Local='' ;
}
fclose ( $this->fp );
$this->Country=preg_replace("/(CZ88.NET)|(純真網路)/","",$this->Country);
$this->Local=preg_replace("/(CZ88.NET)|(純真網路)/","",$this->Local);
//////////////看看 $nRet在上面的值是什麼0和3,於是將下面的行注釋掉
return $nRet ;

//return "$this->Country $this->Local";#如此直接返回位置和國家便可以了
}

function IpToInt($Ip) {
$array=explode('.',$Ip);
$Int=($array[0] * 256*256*256) + ($array[1]*256*256) + ($array[2]*256) + $array[3];
return $Int;
}
}
function GetIP(){//獲取IP
return $_SERVER[REMOTE_ADDR]?$_SERVER[REMOTE_ADDR]:$GLOBALS[HTTP_SERVER_VARS][REMOTE_ADDR];
}
?>

根據ip獲取物理地址

<?php
include_once('./qqwry.php');
$QQWry=new QQWry;
function get_real_ip(){
$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;
}
function is_ip($str) {
$ip = explode(".", $str);
if (count($ip)<4 || count($ip)>4) return 0;
foreach($ip as $ip_addr) {
if ( !is_numeric($ip_addr) ) return 0;
if ( $ip_addr<0 || $ip_addr>255 ) return 0;
}
return 1;
}
if($_POST['ip']){
$ip=$_POST['ip'];
preg_match('/((w|-)+.)+[a-z]{2,4}/i',$ip) ? $ip=gethostbyname($ip) : $ip;
if(is_ip($ip)){
$ifErr=$QQWry->QQWry($ip);
echo "您查詢的IP:&nbsp;".$ip."<br>IP詳細地址:&nbsp;".$QQWry->Country.$QQWry->Local."<br/>";
}else
{
echo "囧,你輸入的IP,511Cha居然不能查詢到!";
}
}else{
$ip=get_real_ip();
if (($_SERVER["HTTP_CLIENT_IP"]) or ($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ifErr=$QQWry->QQWry($ip);
echo "您的真實P是".$ip."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;來自".$QQWry->Country.$QQWry->Local."<br/>";
$ip=$_SERVER['REMOTE_ADDR'];
$ifErr=$QQWry->QQWry($ip);
echo "您的代理IP是".$ip."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;來自".$QQWry->Country.$QQWry->Local;
}
else{
$ip=$_SERVER['REMOTE_ADDR'];
$ifErr=$QQWry->QQWry($ip);
echo "您的IP是:".$ip."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;來自:".$QQWry->Country.$QQWry->Local;
}
}
?>


也可以向樓上說插入資料庫中,不過qqwry.dat最新版有9m多……

Ⅵ QQWRY怎麼使用

利用 QQWry.Dat 實現 IP 地址高效檢索(PHP),當時是直接從文件里查詢的,雖然在創建類的一個實例後,可以進行多次查詢,不用每次查詢都打開文件,已經很快了,但是我想也許還可以更快點兒,於是我想到了用共享內存。下面這個程序就是我用共享內存來實現的快速查詢類,它在第一個實例被創建時將 QQWry.Dat 文件讀入共享內存,以後每次查詢都直接從共享內存中查找數據,而不需要再讀文件了,現在的查詢效率是原來的 1.5 倍。不過使用這個類要注意一點,這個類是一個 Singleton 類,所以需要用 & IpLocation::getInstance 來返回此類的實例引用,而不要用 new IpLocation 來創建實例,不然就不能保證實例的唯一性了。如果在一個頁面內創建多個 IpLocation 實例的話,你會得到內存錯誤,嚴重情況下可能會使你的伺服器崩潰,因此我才把它定義為一個 Singleton 類。另外這個類的實例被創建一次後,文件內容就被讀入到共享內存中了,因此如果伺服器不重新啟動,內存中的 QQWry.Dat 的文件數據就不會更新。

因為用了共享內存,因此對系統有一定的要求,如果系統是 Windows,系統需要 Windows2000 以上系統,PHP 作為 IIS 的 ISAPI 運行才支持共享內存,或者是 Linux 下 PHP 作為 Apache 模塊運行,CGI 和 CLI 方式下不可以。

Ⅶ php代碼 訪問者IP是中國的就跳轉到中文頁面,不是中國的就跳轉到英文頁面

可能原來伺服器有讀取ip所在地的介面吧,可能你那個判斷不好用了

放到index.php中
<?php
include_once 'class_iplocation.php';
$ip='222.132.78.170';
$iplo= new IpLocation();

$a_provinces=Array (
'0' => '河北',
'1' =>'海南',
'2' => '貴州',
'3' => '廣西',
'4' => '廣東',
'5' => '甘肅',
'6' => '福建',
'7' => '北京',
'8' => '安徽',
'9' => '河南',
'10' => '黑龍江',
'11' => '湖北',
'12' => '湖南',
'13' => '吉林',
'14' => '江蘇',
'15' => '江西',
'16' => '遼寧',
'17' => '內蒙古',
'18' => '寧夏',
'19' => '青海',
'20' => '山東',
'21' => '山西',
'22' => '陝西',
'23' => '上海',
'24' => '四川',
'25' => '天津',
'26' => '西藏',
'27' => '新疆',
'28' => '雲南',
'29' => '浙江',
'30' => '重慶',
'31' => '香港',
'32' => '澳門',
'33' => '台灣'
);
$china='';
$strs=$iplo->getlocation($ip);
//print_r($strs);
//echo $strs['country'];
foreach ($a_provinces as $ci)
{
$pos = strpos($strs['country'],$ci);
if ($pos !== false ) {
echo $ci;
$china=$ci;
break;
}
}
echo $china;
if($china!='')
$home='chinese/index.php';
else
$home='english/index.php';

echo "<script language='javascript'>window.location.href='".$home."';</script>";
?>

說明:1.qqwry.dat在qq文件夾下搜就有哦。這三個文件應該放於同一目錄
2.類文件:class_iplocation.php
<?php
/**
* IP 地理位置查詢類
*
* @author 馬秉堯
* @version 1.5
* @right 2005 CoolCode.CN
*/
class IpLocation {
/**
* QQWry.Dat文件指針
*
* @var resource
*/
var $fp;

/**
* 第一條IP記錄的偏移地址
*
* @var int
*/
var $firstip;

/**
* 最後一條IP記錄的偏移地址
*
* @var int
*/
var $lastip;

/**
* IP記錄的總條數(不包含版本信息記錄)
*
* @var int
*/
var $totalip;

/**
* 返回讀取的長整型數
*
* @access private
* @return int
*/
function getlong() {
//將讀取的little-endian編碼的4個位元組轉化為長整型數
$result = unpack('Vlong', fread($this->fp, 4));
return $result['long'];
}

/**
* 返回讀取的3個位元組的長整型數
*
* @access private
* @return int
*/
function getlong3() {
//將讀取的little-endian編碼的3個位元組轉化為長整型數
$result = unpack('Vlong', fread($this->fp, 3).chr(0));
return $result['long'];
}

/**
* 返回壓縮後可進行比較的IP地址
*
* @access private
* @param string $ip
* @return string
*/
function packip($ip) {
// 將IP地址轉化為長整型數,如果在PHP5中,IP地址錯誤,則返回False,
// 這時intval將Flase轉化為整數-1,之後壓縮成big-endian編碼的字元串
return pack('N', intval(ip2long($ip)));
}

/**
* 返回讀取的字元串
*
* @access private
* @param string $data
* @return string
*/
function getstring($data = "") {
$char = fread($this->fp, 1);
while (ord($char) > 0) { // 字元串按照C格式保存,以\0結束
$data .= $char; // 將讀取的字元連接到給定字元串之後
$char = fread($this->fp, 1);
}
return $data;
}

/**
* 返回地區信息
*
* @access private
* @return string
*/
function getarea() {
$byte = fread($this->fp, 1); // 標志位元組
switch (ord($byte)) {
case 0: // 沒有區域信息
$area = "";
break;
case 1:
case 2: // 標志位元組為1或2,表示區域信息被重定向
fseek($this->fp, $this->getlong3());
$area = $this->getstring();
break;
default: // 否則,表示區域信息沒有被重定向
$area = $this->getstring($byte);
break;
}
return $area;
}

/**
* 根據所給 IP 地址或域名返回所在地區信息
*
* @access public
* @param string $ip
* @return array
*/
function getlocation($ip) {
if (!$this->fp) return null; // 如果數據文件沒有被正確打開,則直接返回空
$location['ip'] = gethostbyname($ip); // 將輸入的域名轉化為IP地址
$ip = $this->packip($location['ip']); // 將輸入的IP地址轉化為可比較的IP地址
// 不合法的IP地址會被轉化為255.255.255.255
// 對分搜索
$l = 0; // 搜索的下邊界
$u = $this->totalip; // 搜索的上邊界
$findip = $this->lastip; // 如果沒有找到就返回最後一條IP記錄(QQWry.Dat的版本信息)
while ($l <= $u) { // 當上邊界小於下邊界時,查找失敗
$i = floor(($l + $u) / 2); // 計算近似中間記錄
fseek($this->fp, $this->firstip + $i * 7);
$beginip = strrev(fread($this->fp, 4)); // 獲取中間記錄的開始IP地址
// strrev函數在這里的作用是將little-endian的壓縮IP地址轉化為big-endian的格式
// 以便用於比較,後面相同。
if ($ip < $beginip) { // 用戶的IP小於中間記錄的開始IP地址時
$u = $i - 1; // 將搜索的上邊界修改為中間記錄減一
}
else {
fseek($this->fp, $this->getlong3());
$endip = strrev(fread($this->fp, 4)); // 獲取中間記錄的結束IP地址
if ($ip > $endip) { // 用戶的IP大於中間記錄的結束IP地址時
$l = $i + 1; // 將搜索的下邊界修改為中間記錄加一
}
else { // 用戶的IP在中間記錄的IP范圍內時
$findip = $this->firstip + $i * 7;
break; // 則表示找到結果,退出循環
}
}
}

//獲取查找到的IP地理位置信息
fseek($this->fp, $findip);
$location['beginip'] = long2ip($this->getlong()); // 用戶IP所在范圍的開始地址
$offset = $this->getlong3();
fseek($this->fp, $offset);
$location['endip'] = long2ip($this->getlong()); // 用戶IP所在范圍的結束地址
$byte = fread($this->fp, 1); // 標志位元組
switch (ord($byte)) {
case 1: // 標志位元組為1,表示國家和區域信息都被同時重定向
$countryOffset = $this->getlong3(); // 重定向地址
fseek($this->fp, $countryOffset);
$byte = fread($this->fp, 1); // 標志位元組
switch (ord($byte)) {
case 2: // 標志位元組為2,表示國家信息又被重定向
fseek($this->fp, $this->getlong3());
$location['country'] = $this->getstring();
fseek($this->fp, $countryOffset + 4);
$location['area'] = $this->getarea();
break;
default: // 否則,表示國家信息沒有被重定向
$location['country'] = $this->getstring($byte);
$location['area'] = $this->getarea();
break;
}
break;
case 2: // 標志位元組為2,表示國家信息被重定向
fseek($this->fp, $this->getlong3());
$location['country'] = $this->getstring();
fseek($this->fp, $offset + 8);
$location['area'] = $this->getarea();
break;
default: // 否則,表示國家信息沒有被重定向
$location['country'] = $this->getstring($byte);
$location['area'] = $this->getarea();
break;
}
if ($location['country'] == " CZ88.NET") { // CZ88.NET表示沒有有效信息
$location['country'] = "未知";
}
if ($location['area'] == " CZ88.NET") {
$location['area'] = "";
}
return $location;
}

/**
* 構造函數,打開 QQWry.Dat 文件並初始化類中的信息
*
* @param string $filename
* @return IpLocation
*/
function IpLocation($filename = "QQWry.Dat") {
$this->fp = 0;
if (($this->fp = @fopen($filename, 'rb')) !== false) {
$this->firstip = $this->getlong();
$this->lastip = $this->getlong();
$this->totalip = ($this->lastip - $this->firstip) / 7;
//注冊析構函數,使其在程序執行結束時執行
register_shutdown_function(array(&$this, '_IpLocation'));
}
}

/**
* 析構函數,用於在頁面執行結束後自動關閉打開的文件。
*
*/
function _IpLocation() {
if ($this->fp) {
fclose($this->fp);
}
$this->fp = 0;
}
}
?>

熱點內容
sqlbrowser服務 發布:2025-02-02 01:32:14 瀏覽:729
jeecg源碼下載 發布:2025-02-02 01:08:47 瀏覽:365
多台電腦能共用一個伺服器嗎 發布:2025-02-02 01:08:44 瀏覽:26
oppo雲存儲空間怎麼清理 發布:2025-02-02 00:59:31 瀏覽:263
項目編譯啥意思 發布:2025-02-02 00:25:13 瀏覽:223
逐鹿中原怎樣做掛機腳本 發布:2025-02-02 00:23:39 瀏覽:28
安卓系統跟蹤在哪裡 發布:2025-02-02 00:23:38 瀏覽:899
安卓uc瀏覽器怎麼安裝油猴 發布:2025-02-02 00:23:38 瀏覽:299
中商情報網資料庫 發布:2025-02-02 00:09:26 瀏覽:533
python獲取目錄下所有文件名 發布:2025-02-01 23:47:54 瀏覽:896