PHP method for determining IP addresses and redirecting to corresponding city substations _ PHP Tutorial

Source: Internet
Author: User
PHP determines the IP address and redirects it to the corresponding City substation. This article mainly introduces how PHP judges and redirects IP addresses to corresponding sub-stations. The example analyzes PHP's URL parsing and redirection skills, a php method for determining IP addresses and redirecting to corresponding city substations

This article mainly introduces how PHP judges IP addresses and redirects them to the corresponding sub-stations in cities. The example analyzes php URL parsing and redirection skills, which has some reference value. For more information, see

This article describes how PHP judges IP addresses and redirects them to the corresponding sub-stations. Share it with you for your reference. The specific implementation method is as follows:

?

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

Class QQWry {

Var $ StartIP = 0;

Var $ EndIP = 0;

Var $ Country = '';

Var $ Local = '';

Var $ CountryFlag = 0;

// Identify the Country location

// 0x01, followed by a Country offset of 3 bytes without Local

// 0x02, followed by the Country offset in 3 bytes, followed by The Local

// Others, Country, Local, and Local have similar compression. Multiple references are allowed.

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 (! Is_string ($ dotip) | $ dotip = '') {return ;}

If (preg_match ("/^ 127/", $ dotip) {$ this-> Country = "local network"; return ;}

Elseif (preg_match ("/^ 192/", $ dotip) {$ this-> Country = "LAN"; 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 is not very good ..........

$ This-> Local = str_replace ("area error", "", $ this-> Local );

}

Else {

$ NRet = 3;

$ This-> Country = 'unknown ';

$ This-> Local = '';

}

Fclose ($ this-> fp );

$ This-> Country = preg_replace ("/(CZ88.NET) | (pure network)/", "Lan/unknown", $ this-> Country );

$ This-> Local = preg_replace ("/(CZ88.NET) | (pure network)/", "Lan/unknown", $ this-> Local );

///// // Check the values 0 and 3 of $ nRet. then, comment out the following lines.

Return $ nRet;

// Return "$ this-> Country $ this-> Local"; # return the location and Country directly.

}

Function IpToInt ($ Ip ){

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

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

Return $ Int;

}

}

?>

The example is as follows:

?

1

2

3

4

$ QQWry = new QQWry;

$ QQWry-> QQWry ("60.31.95.255 ");

$ Country = $ QQWry-> Country;

Echo $ QQWry-> Country. ",". $ QQWry-> Local;

I hope this article will help you with php programming.

This article describes how to use PHP to determine the IP address and switch to the corresponding sub-station. The example analyzes the php URL parsing and jump skills...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.