2004年08月03日


define
(‘QQWRY’ , $qqwry_root_path . ‘QQwry.dat’ ) ;

function IpToInt($Ip) {

    $array=explode(‘.’,$Ip);

    $Int=($array[0] * 256*256*256) + ($array[1]*256*256) + ($array[2]*256) + $array[3];

    return $Int;

}

function IntToIp($Int) {

    $b1=($Int & 0xff000000)>>24;

    if ($b1<0) $b1+=0×100;

    $b2=($Int & 0×00ff0000)>>16;

    if ($b2<0) $b2+=0×100;

    $b3=($Int & 0×0000ff00)>>8;

    if ($b3<0) $b3+=0×100;

    $b4= $Int & 0×000000ff;

    if ($b4<0) $b4+=0×100;

    $Ip=$b1.‘.’.$b2.‘.’.$b3.‘.’.$b4;

    return $Ip;

}

class TQQwry

{

    var $StartIP = 0;

    var $EndIP   = 0;

    var $Country = ;

    var $Local   = ;

    var $CountryFlag = 0; // 标识 Country位置

                          // 0×01,随后3字节为Country偏移,没有Local

                          // 0×02,随后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 ) ;

            if ( ord ( $c[0] ) == 0  )

               break ;

            $str .= $c ;

        }

        return $str ;

    }

    function qqwry ($dotip) {

        $nRet;

        $ip = 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 ) ;

        return $nRet ;

    }

}

function ip2location ( $ip )

{

    $wry = new TQQwry ;

    $nRet = $wry->qqwry ( $ip );

    //可以利用 $nRet做一些事情,我是让他自动记录未知IP到一个表,代码就不写了。

  return $wry->Country.$wry->Local ;

}

?>

<%
” **********************************************************

‘ 利用QQ的IP查询数据库查询IP所在地ASP源码

‘ **********************************************************
‘ 【程序说明】
‘ 本程序是由Strongc的PHP程序改编而来,改写程序的目的主要在
‘ 于学习,请不要用于商业等用途,如有改变请通知原作者和我本人。
‘ 以下是原作者要求保留信息,大家使用本程序时,为了尊重作者,
‘ 请给予保留,谢谢!
‘ =====================================================
‘ QQwry.dat格式分析和查询IP位置的PHP程序
‘ By Strongc http://strongc.51.net/d2x/

‘ 转载时不要去掉我的名字和我的主页链接,谢谢!

‘ ======================================================
‘ 作者:东仔(欢迎交流,Email:liudong.163@163.com)
‘ 2003年8月

‘【使用说明】
‘ 为了便于测试,就保留前端页面脚本模式,如有需要可以把本部分程序修
‘ 改为Server脚本就OK,具体请看内部注释。

Class TQQwry
Dim Country,LocalStr
Private StartIP,EndIP,CountryFlag
Private FirstStartIp,LastStartIp,EndIpOff
Private Stream,QQwryFile

Private Sub Class_Initialize
Country=”"
LocalStr=”"

StartIP=0
EndIP=0

CountryFlag=0 ‘标识 Country位置
‘ 0×01,随后3字节为Country偏移,没有LocalStr
‘ 0×02,随后3字节为Country偏移,接着是LocalStr
‘ 其他,Country,LocalStr,LocalStr有类似的压缩。可能多重引用。
FirstStartIp=0
LastStartIp=0
EndIpOff=0

QQwryFile=”QQwry.dat” ‘QQ IP库路径,请手动设置!!!
End Sub

function IpToInt(IP)
if trim(IP)=”" then
IpToInt=0
exit function
end if

IpArray=Split(IP,”.”,-1)
if UBound(IpArray)<3 then
IpToInt=IpToInt(IP&”.0″)
exit function
end if

for i=0 to 3
if not IsNumeric(IpArray(i)) then IpArray(i)=0
if CInt(IpArray(i))<0 then IpArray(i)=Abs(CInt(IpArray(i)))
if CInt(IpArray(i))>255 then IpArray(i)=255
next

IpToInt=(CInt(IpArray(0))*256*256*256) + (CInt(IpArray(1))*256*256) + (CInt(IpArray(2))*256) + CInt(IpArray(3))
End function

function IntToIp(IntValue)
p4=IntValue-Fix(IntValue/256)*256
IntValue=(IntValue-p4)/256

p3=IntValue-Fix(IntValue/256)*256
IntValue=(IntValue-p3)/256

p2=IntValue-Fix(IntValue/256)*256
IntValue=(IntValue-p2)/256

p1=IntValue

IntToIp=Cstr(p1)&”.”&Cstr(p2)&”.”&Cstr(p3)&”.”&Cstr(p4)
end function

Private function getStartIp(RecNo)
offset = FirstStartIp + RecNo * 7
Stream.Position = offset
buf = Stream.Read(7)

EndIpOff = AscB(midb(buf,5,1)) + (AscB(midb(buf,6,1))*256) + (AscB(midb(buf,7,1))* 256*256)
StartIp = AscB(midb(buf,1,1)) + (AscB(midb(buf,2,1))*256) + (AscB(midb(buf,3,1))*256*256) + (AscB(midb(buf,4,1))*256*256*256)
getStartIp=StartIp
end function

Private function getEndIp()
Stream.Position = EndIpOff
buf = Stream.Read(5)
EndIp = AscB(midb(buf,1,1)) + (AscB(midb(buf,2,1))*256) + (AscB(midb(buf,3,1))*256*256) + (AscB(midb(buf,4,1))*256*256*256)
CountryFlag = AscB(midb(buf,5,1))
getEndIp=EndIp
end function

Private sub getCountry()
if (CountryFlag=1 or CountryFlag=2) then
Country = getFlagStr (EndIpOff+4)
if CountryFlag=1 then
LocalStr=”"
else
LocalStr=getFlagStr (EndIpOff+8)
end if
else
Country = getFlagStr(EndIpOff+4)
LocalStr = getFlagStr (Stream.Position)
end if
end sub

Private function getFlagStr(offset)
flag = 0
do while (true)
Stream.Position=offset
flag = AscB(Stream.Read(1))
if(flag = 1 or flag = 2 ) then
buf = Stream.Read(3)
if (flag = 2 ) then
CountryFlag = 2
EndIpOff = offset – 4
end if
offset = AscB(midb(buf,1,1)) + (AscB(midb(buf,2,1))*256) + (AscB(midb(buf,3,1))* 256*256)
else
exit do
end if
loop

if (offset < 12 ) then
getFlagStr=”"
else
Stream.Position=offset
getFlagStr=getStr()
end if
end function

Private function getStr()
getStr=”"
do while (true)
c=AscB(Stream.Read(1))
if (c=0) then exit do

‘如果是双字节,就进行高字节在结合低字节合成一个字符
If c > 127 Then
If Stream.EOS then Exit do
getStr=getStr&Chr(AscW(ChrB(AscB(Stream.Read(1)))&ChrB(C)))
Else
getStr=getStr&Chr(c)
End If
loop
end function

Public function qqwry(dotip)
dim nRet
ip = IpToInt (dotip)

‘判断是否属于特殊IP
if ip>=IpToInt(“127.0.0.0″) and ip<=IpToInt(“127.255.255.255″) then
Country=”本机内部环回地址”
LocalStr=”"
nRet=1
exit function
elseif (ip>=IpToInt(“0.0.0.0″) and ip<=IpToInt(“2.255.255.255″)) _
or (ip>=IpToInt(“64.0.0.0″) and ip<=IpToInt(“126.255.255.255″)) _
or (ip>=IpToInt(“58.0.0.0″) and ip<=IpToInt(“60.255.255.255″)) then
Country=”网络保留地址”
LocalStr=”"
nRet=0
exit function
end if

set Stream=CreateObject(“Adodb.Stream”)
Stream.mode=3
Stream.type=1
Stream.open
Stream.LoadFromFile QQwryFile
Stream.Position=0
buf = Stream.Read(8)

FirstStartIp = AscB(midb(buf,1,1)) + (AscB(midb(buf,2,1))*256) + (AscB(midb(buf,3,1))*256*256) + (AscB(midb(buf,4,1))*256*256*256)

LastStartIp = AscB(midb(buf,5,1)) + (AscB(midb(buf,6,1))*256) + (AscB(midb(buf,7,1))*256*256) + (AscB(midb(buf,8,1))*256*256*256)

RecordCount= Int( (LastStartIp – FirstStartIp)/7)
if (RecordCount <=1) then
Country = “FileDataError”
qqwry=2
exit function
end if

RangB= 0
RangE= RecordCount

do while (RangB < RangE-1)
RecNo= Int((RangB + RangE)/2)
Call getStartIp (RecNo)
if (ip=StartIp) then
RangB = RecNo
exit do
end if
if (ip > StartIp) then
RangB= RecNo
else
RangE= RecNo
end if
loop

Call getStartIp (RangB)
Call getEndIp()

if((StartIp<=ip) and ( EndIp >=ip)) then
nRet = 0
call getCountry()
if ip>=IpToInt(“10.84.102.0″) and ip<=IpToInt(“10.84.103.255″) then
Country=Country&”或山东省”
LocalStr=”荷泽市(山东169电信用户)”
elseif ip>=IpToInt(“10.150.5.92″) and ip<=IpToInt(“10.150.5.92″) then
Country=Country&”或江苏省”
LocalStr=”常州市”
elseif ip>=IpToInt(“10.0.0.0″) and ip<=IpToInt(“10.255.255.255″) then
Country=Country&”或未知”
LocalStr=”"
end if
else
nRet=3
Country = “未知”
LocalStr = “”
end if
qqwry=nRet
end function

Private Sub Class_Terminate
On Error Resume Next
Stream.close
if Err.number<>0 then Err.Clear
set Stream=nothing
End Sub
end class

‘返回IP信息
function ip2location (ip)
set wry =new TQQwry
nRet = wry.qqwry(ip)
‘可以利用nRet做一些事情,我是让他自动记录未知IP到一个表,代码就不写了。
ip2location=wry.Country&wry.LocalStr
end function

‘这是一个前端过程,改为ASP应注意修改!

IP=request(“IpValue”)
t1=Timer*1000
response.write “该IP属于:”&ip2location(IP)&”IP(耗时:”&(Timer*1000-t1)&”ms)”

%>
<center>
<table border=”5″ cellPadding=5 width=”400″>
<tr>
<td>
<pre style=”line-height:120%”>
**********************************************************

<h4 align=”center”>利用QQ的IP查询数据库查询IP所在地ASP源码</h4>

**********************************************************
【程序说明】
本程序是由Strongc的PHP程序改编而来,改写程序的目的主要在
于学习,请不要用于商业等用途,如有改变请通知原作者和我本人。
以下是原作者要求保留信息,大家使用本程序时,为了尊重作者,
请给予保留,谢谢!
<span style=”color:#568B6F”>
<a href=”http://www.e99e.com/cgi-bin/topic.cgi?forum=27&topic=301&show=75″ target=”_blank” title=”参看分析”>QQwry.dat格式分析和查询IP位置的PHP程序</a>
By Strongc <a href=”http://strongc.51.net/d2x/” target=”_blank”>http://strongc.51.net/d2x/</a>

转载时不要去掉我的名字和我的主页链接,谢谢!
</span>
作者:<a href=”mailto:liudong.963@163.com”>东仔</a>
2003年8月</pre></td>
</tr>
</table>
<br><br><form method=”post” action=”ip.asp”>
<input type=”text” value=”127.0.0.1″ name=”IpValue”> 
<input type=”submit” value=”查询” >
<input type=”reset” value=”Reset”>
</form>
</center>

2004年08月02日

    在 ASP 中使用 Request.ServerVariables(“REMOTE_ADDR”) 来取得客户端的 IP 地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的 IP 地址,而不是真正的客户端 IP 地址。要想透过代理服务器取得客户端的真实 IP 地址,就要使用 Request.ServerVariables(“HTTP_X_FORWARDED_FOR”) 来读取。
  不过要注意的事,并不是每个代理服务器都能用 Request.ServerVariables(“HTTP_X_FORWARDED_FOR”) 来读取客户端的真实 IP,有些用此方法读取到的仍然是代理服务器的 IP。
  还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables(“HTTP_X_FORWARDED_FOR”) 取到的值将是空的。因此,如果要在程序中使用此方法,可以这样处理:

……
userip = Request.ServerVariables(“HTTP_X_FORWARDED_FOR”)
If userip = “” Then userip = Request.ServerVariables(“REMOTE_ADDR”)
……

  即:如果客户端通过代理服务器,则取 HTTP_X_FORWARDED_FOR 的值,如果没通过代理服务器,就取 REMOTE_ADDR 的值。  

2004年06月18日

在PHP中连接ACCESS有三种方式:
1、创建系统数据源,用php提供的odbc函数即可
2、同样使用php的odbc函数,但不创建数据源。
$connstr=”DRIVER=Microsoft Access Driver (*.mdb);DBQ=”.realpath(“netBook.mdb”) ;
$connid=odbc_connect($connstr,”",”",SQL_CUR_USE_ODBC );
3、使用微软的ADODB数据库驱动
$conn = new com(“ADODB.Connection”);
$connstr = “DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=” . realpath(“netBook.mdb”);
$conn->Open($connstr);
$rs = new com(“ADODB.RecordSet”);
$rs->Open(“select * from class”,$conn,1,1);