-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.php
124 lines (113 loc) · 3.43 KB
/
sample.php
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
#!/usr/bin/php
<?php
if ( ! extension_loaded ('geoip') ) {
if ( version_compare (PHP_VERSION, '5.4.0', '>=') ) {
fprintf (STDERR, "GeoIP extension is not loaded\n");
exit (1);
} else if ( version_compare (PHP_VERSION, '5.3.0', '>=') )
dl ('geoip.so');
else
dl ('modules/geoip.so');
}
$searches = array ('oops.org', 'kornet.net', 'yahoo.com');
/*
* resource GeoIP_open (database, flag)
* resource GeoIP_open (database_code, flag)
* resource GeoIP_open (flag)
* resource GeoIP_open ()
*
* open the geoip database
*
* argument info:
* database => GeoIP datafile path or database code
* database code :
* GEOIP_COUNTRY_EDITION -> default
* GEOIP_ISP_EDITION -> isp database (if you have)
* GEOIP_CITY_EDITION_REV0 -> city database (if you have)
* GEOIP_CITY_EDITION_REV1 -> city database (if you have)
* GEOIP_ORG_EDITION -> org database (if you have)
*
* flag => GEOIP_STANDARD
* GEOIP_MEMORY_CACHE
* GEOIP_CHECK_CACHE
* GEOIP_INDEX_CACHE
*
* if flag is not specifyed, default "GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE"
*
* if failed, return FALSE
*
*/
try {
/* open conutry database */
$g = GeoIP_open (GEOIP_MEMORY_CACHE|GEOIP_CHECK_CACHE);
/* open city database */
if ( GeoIP_db_avail (GEOIP_CITY_EDITION_REV1) )
$gc = GeoIP_open (GEOIP_CITY_EDITION_REV1, GEOIP_INDEX_CACHE|GEOIP_CHECK_CACHE);
/* open isp database */
if ( GeoIP_db_avail (GEOIP_ISP_EDITION) )
$gi = GeoIP_open (GEOIP_ISP_EDITION, GEOIP_INDEX_CACHE|GEOIP_CHECK_CACHE);
if ( ! is_resource ($g) )
exit;
#echo "TYPE: " . geoip_database_info ($g) ."\n";
foreach ( $searches as $v ) {
/*
* search geoip country database
*
* array geoip_id_by_name (geoip handle, host);
* return:
* array (
* country_id,
* code, // code of nation
* name // name of nation
* );
*
* string geoip_country_code_by_name (geoip handle, host)
* string geoip_country_code_by_name (geoip handle, host)
*
*/
$r = geoip_id_by_name ($g, $v);
print_r ($r);
/* print city information
*
* array geoip_record_by_name (geoip handle, host)
* return:
* array (
* country_code, // country 2 digit code
* region, // states or do (korean)
* city, // name of city
* postal_code, // postal code or post code
* latitude,
* longitude,
* if ( DB is GEOIP_CITY_EDITION_REV1 ) {
* dma_code,
* area_code,
* }
* );
*/
if ( is_resource ($gc) ) {
$rc = GeoIP_record_by_name ($gc, $v);
print_r ($rc);
}
/* print isp information
*
* string geoip_org_by_name (geoip handle, host)
*/
if ( is_resource ($gi) ) {
$ri = GeoIP_org_by_name ($gi, $v);
echo " $ri\n";
}
#echo "### " . geoip_country_code_by_name ($g, $v) . "\n";
#echo "### " . geoip_country_name_by_name ($g, $v) . "\n";
}
/*
* close geoip database
*/
GeoIP_close ($g);
if ( is_resource ($gc) ) GeoIP_close ($gc);
if ( is_resource ($gi) ) GeoIP_close ($gi);
} catch ( GeoIPException $e ) {
fprintf (STDERR, "%s\n", $e->getMessage ());
$err = preg_split ('/\r?\n/', $e->getTraceAsString ());
print_r ($err);
}
?>