13
13
require_once __DIR__ .'/geoip2.phar ' ;
14
14
use GeoIp2 \Database \Reader ;
15
15
16
- //// ipCheck
16
+ //// getDownloadRegion
17
17
// Lookup the IP, and classify it by:
18
18
// - Continent
19
19
// - Country
20
20
// - Timezone
21
21
// Returns either a string of the selected region,
22
22
// or an array of two regions.
23
- function ipCheck ($ hostname , $ debug = false ) {
23
+ function getDownloadRegion ($ hostname , $ debug = false ) {
24
24
25
25
try {
26
- if ( $ debug ) {
27
- echo $ hostname ."\n" ;
28
- }
29
26
if (!class_exists ('GeoIp2\Database\Reader ' )) {
30
27
throw new \Exception ('Class GeoIp2\Database\Reader not found ' );
31
28
}
32
29
$ reader = new Reader (__DIR__ .'/GeoLite2-City.mmdb ' );
33
30
$ record = $ reader ->city ($ hostname );
34
- if ( $ debug ) {
35
- var_dump ($ record );
36
- echo "\n" ;
37
- }
31
+
38
32
$ continent = $ record ->continent ->code ;
39
33
$ country = $ record ->country ->isoCode ;
40
- $ longitude = $ record ->location ->longitude ;
34
+ $ longitude = $ record ->location ->longitude ;
41
35
42
36
} catch (\Exception $ e ) {
43
37
if ( $ debug ) {
@@ -48,12 +42,12 @@ function ipCheck($hostname, $debug = false) {
48
42
49
43
$ continent = false ;
50
44
$ country = false ;
51
- $ longitude = false ;
45
+ $ longitude = false ;
52
46
}
53
47
54
48
if ( $ debug ) {
55
49
echo 'Continent: " ' .$ continent .'" ' ."\n" ;
56
- echo 'Country: " ' .$ country .'" ' ."\n" ;
50
+ echo 'Country: " ' .$ country .'" ' ."\n" ;
57
51
echo 'Longitude: " ' .$ longitude .'" ' ."\n" ;
58
52
}
59
53
@@ -118,10 +112,10 @@ function ipCheck($hostname, $debug = false) {
118
112
119
113
}
120
114
121
- //// ipHash
115
+ //// getIPHash
122
116
// Hashes the given IP to return either a 0 or a 1 consistently for the same IP.
123
- // Used when balancing between two regions returned by ipCheck .
124
- function ipHash ($ hostname , $ debug = false ) {
117
+ // Used when balancing between two regions returned by getDownloadRegion .
118
+ function getIPHash ($ hostname , $ debug = false ) {
125
119
$ hash = array_sum (str_split ($ hostname ));
126
120
if ( $ debug ) {
127
121
echo 'Hash: " ' .$ hash .'" ' ."\n" ;
@@ -140,18 +134,12 @@ function ipHash($hostname, $debug = false) {
140
134
function getCurrentCountry ($ hostname , $ debug = false ) {
141
135
142
136
try {
143
- if ( $ debug ) {
144
- echo $ hostname ."\n" ;
145
- }
146
137
if (!class_exists ('GeoIp2\Database\Reader ' )) {
147
138
throw new \Exception ('Class GeoIp2\Database\Reader not found ' );
148
139
}
149
140
$ reader = new Reader (__DIR__ .'/GeoLite2-City.mmdb ' );
150
141
$ record = $ reader ->city ($ hostname );
151
- if ( $ debug ) {
152
- var_dump ($ record );
153
- }
154
- $ country = $ record ->country ->isoCode ;
142
+ $ country = $ record ->country ->isoCode ;
155
143
156
144
} catch (\Exception $ e ) {
157
145
if ( $ debug ) {
@@ -168,3 +156,51 @@ function getCurrentCountry($hostname, $debug = false) {
168
156
169
157
return $ country ;
170
158
}
159
+
160
+
161
+ function getCurrentLocation ($ hostname , $ debug = false ) {
162
+
163
+ try {
164
+ if ( $ debug ) {
165
+ echo $ hostname ."\n" ;
166
+ }
167
+ if (!class_exists ('GeoIp2\Database\Reader ' )) {
168
+ throw new \Exception ('Class GeoIp2\Database\Reader not found ' );
169
+ }
170
+ $ reader = new Reader (__DIR__ .'/GeoLite2-City.mmdb ' );
171
+ $ record = $ reader ->city ($ hostname );
172
+
173
+ $ city = $ record ->city ->name ; // 'Minneapolis'
174
+ $ state = $ record ->mostSpecificSubdivision ->name ; // 'Minnesota'
175
+ $ stateCode = $ record ->mostSpecificSubdivision ->isoCode ; // 'MN'
176
+ $ country = $ record ->country ->name ; // 'United States'
177
+ $ countryCode = $ record ->country ->isoCode ; // 'US'
178
+ $ postcode = $ record ->postal ->code ; // '55455'
179
+ $ continent = $ record ->continent ->code ;
180
+
181
+ } catch (\Exception $ e ) {
182
+ if ( $ debug ) {
183
+ echo $ e ->getMessage ();
184
+ } else {
185
+ error_log ($ e ->getMessage ());
186
+ }
187
+
188
+ $ city = false ;
189
+ $ state = false ;
190
+ $ stateCode = false ;
191
+ $ country = false ;
192
+ $ countryCode = false ;
193
+ $ postcode = false ;
194
+ $ continent = false ;
195
+ }
196
+
197
+ return array (
198
+ 'city ' => $ city ,
199
+ 'state ' => $ state ,
200
+ 'stateCode ' => $ stateCode ,
201
+ 'country ' => $ country ,
202
+ 'countryCode ' => $ countryCode ,
203
+ 'postcode ' => $ postcode ,
204
+ 'continent ' => $ continent ,
205
+ );
206
+ }
0 commit comments