PHP を使用して IP を特定し、対応する市の支店にリダイレクトする方法_PHP チュートリアル

WBOY
リリース: 2016-07-13 10:00:19
オリジナル
825 人が閲覧しました

PHPを使用してIPを決定し、対応する都市サブステーションにリダイレクトする方法

この記事では、主にPHPでIPを決定し、対応する都市サブステーションにリダイレクトする方法を紹介し、PHPのテクニックを分析します。 URLを解析して例にジャンプすると、特定の参考値があり、必要な友達が参照できます

この記事の例では、PHP がどのように IP を決定し、対応する都市の変電所にリダイレクトするかを説明します。参考のためにみんなで共有してください。具体的な実装方法は以下の通りです

?

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

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

クラスQQWry{

var $StartIP=0;

var $EndIP=0;

var $ Country='';

var $Local='';

var $ CountryFlag=0;

// 国の場所をマークします

// 0x01、その後に国オフセットの 3 バイトが続き、ローカルなし

// 0x02、その後に国オフセットの 3 バイト、その後にローカル

// その他、国、ローカル、ローカルでも同様の圧縮があります。複数の参照の可能性があります。

var $fp;

var $FirstStartIp=0;

var $LastStartIp=0;

var $EndIpOff=0 ;

関数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);

$this->StartIp を返す;

}

関数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] ) ;

$this->EndIp を返す ;

}

関数 get Country(){

スイッチ ( $this->国旗 ) {

ケース1:

ケース2:

$this-> Country=$this->getFlagStr ( $this->EndIpOff+4) ;

//echo sprintf('EndIpOffset=(%x)',$this->EndIpOff );

$this->ローカル=( 1 == $this->国フラグ)? '' : $this->getFlagStr ( $this->EndIpOff+8);

休憩;

デフォルト:

$this-> Country=$this->getFlagStr ($this->EndIpOff+4);

$this->Local=$this->getFlagStr ( ftell ( $this->fp ));

}

}

関数 getFlagStr ($offset){

$フラグ=0 ;

その間(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->国旗=2;

$this->EndIpOff=$offset - 4 ;

}

$offset=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])* 256*256);

}

その他{

休憩;

}

}

if($offset

戻る '';

@fseek($this->fp , $offset , SEEK_SET ) ;

return $this->getStr();

}

関数getStr()

{

$str='' ;

ながら ( 1 ) {

$c=fgetc ( $this->fp ) ;

//エコー "$cn" ;

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

休憩;

$str.= $c ;

}

//エコー "$str n";

$str を返します ;

}

関数 qqwry ($dotip='') {

if( !is_string($dotip) || $dotip==''){return;}

if(preg_match("/^127/",$dotip)){$this-> Country="本地网络";return ;}

elseif(preg_match("/^192/",$dotip)) {$this-> Country="局域网";return ;}

$nRet;

$ip=$this->IpToInt ( $dotip );

$this->fp= fopen(__QQWRY__, "rb");

if ($this->fp == NULL) {

$szLocal= "OpenFileError";

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= フロア(($this->LastStartIp - $this->FirstStartIp) / 7);

if ($RecordCount

$this->国="ファイルデータエラー";

fclose($this->fp) ;

2 を返す ;

}

$RangB= 0;

$RangE= $RecordCount;

// 一致...

その間 ($RangB

{

$RecNo= フロア(($RangB + $RangE) / 2);

$this->getStartIp ( $RecNo ) ;

if ( $ip == $this->StartIp )

{

$RangB=$RecNo ;

休憩;

}

if ($ip>$this->StartIp)

$RangB= $RecNo;

その他

$RangE= $RecNo;

}

$this->getStartIp ( $RangB ) ;

$this->getEndIp ( ) ;

if ( ( $this->StartIp <= $ip ) && ( $this->EndIp >= $ip ) ){

$nRet=0 ;

$this->get Country ( ) ;

//这样不太好…………所以…………

$this->Local=str_replace("エリアエラー", "",$this->Local);

}

その他{

$nRet=3 ;

$this->国='不明' ;

$this->ローカル='' ;

}

fclose ( $this->fp );

$this-> Country=preg_replace("/(CZ88.NET)|(無害なネットワーク)/","LAN/不明",$this-> Country);

$this->Local=preg_replace("/(CZ88.NET)|(無害なネットワーク)/","LAN/不明",$this->Local);

//////////////上記の $nRet の値 0 と 3 を見て、次の行をコメントアウトします

$nRet を返す ;

//return "$this->country $this->Local";# 場所と国を直接返すだけです

}

関数 IpToInt($Ip) {

$array=explode('.',$Ip);

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

$Int を返す;

}

}

?>

デモの例は次のとおりです:

?

1

2

3

4

$QQWry=新しい QQWry;

$QQWry->QQWry("60.31.95.255");

$country = $QQWry->国;

エコー $QQWry->国。",".$QQWry->ローカル;

この記事で説明した内容が皆様の PHP プログラミング設計に役立つことを願っています。

www.bkjia.com本当http://www.bkjia.com/PHPjc/973923.html技術記事この記事では主に、PHP で IP を決定し、対応する都市のサブステーションにジャンプする方法を紹介します。PHP による URL の解析とジャンプのテクニックを例を用いて分析します。
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!