├── README.md
├── index.php
├── ip_to_country.sql
└── phpip2country.class.php
/README.md:
--------------------------------------------------------------------------------
1 | # phpIp2Country
2 |
3 | ###PHP geolocalization class with free (updated daily) IPs database
4 |
5 | Example code:
6 |
7 |
8 | require('phpip2country.class.php');
9 |
10 | $dbConfigArray = array(
11 | 'host' => 'localhost', //example host name
12 | 'port' => 3306, //3306 -default mysql port number
13 | 'dbName' => 'ip_to_country', //example db name
14 | 'dbUserName' => 'ip_to_country', //example user name
15 | 'dbUserPassword' => 'QrDB9Y8CKMdLDH8Q', //example user password
16 | 'tableName' => 'ip_to_country', //example table name
17 | );
18 |
19 | $phpIp2Country = new phpIp2Country('213.180.138.148',$dbConfigArray);
20 |
21 | print_r($phpIp2Country->getInfo(IP_INFO));
22 | ?>
23 |
24 | Outputs:
25 |
26 | Array(
27 | [IP_FROM] => 3585376256
28 | [IP_TO] => 3585384447
29 | [REGISTRY] => RIPE
30 | [ASSIGNED] => 948758400
31 | [CTRY] => PL
32 | [CNTRY] => POL
33 | [COUNTRY] => POLAND
34 | [IP_STR] => 213.180.138.148
35 | [IP_VALUE] => 3585378964
36 | [IP_FROM_STR] => 127.255.255.255
37 | [IP_TO_STR] => 127.255.255.255
38 | )
39 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 | if(!empty($_POST['ip'])){
11 | require('./phpip2country.class.php');
12 |
13 | /**
14 | * Newest data (SQL) avaliable on project website
15 | * @link http://code.google.com/p/php-ip-2-country/
16 | */
17 | $dbConfigArray = array(
18 | 'host' => 'localhost', //example host name
19 | 'port' => 3306, //3306 -default mysql port number
20 | 'dbName' => 'ip_to_country', //example db name
21 | 'dbUserName' => 'ip_to_country', //example user name
22 | 'dbUserPassword' => 'QrDB9Y8CKMdLDH8Q', //example user password
23 | 'tableName' => 'ip_to_country', //example table name
24 | );
25 |
26 | $phpIp2Country = new phpIp2Country($_POST['ip'],$dbConfigArray);
27 |
28 | echo 'IP: ' . $phpIp2Country->getInfo(IP_STR);
29 | echo '
';
30 | echo 'IP numerical Value: ' . $phpIp2Country->getInfo(IP_VALUE);
31 | echo '
';
32 | echo 'IP registry: ' . $phpIp2Country->getInfo(IP_REGISTRY);
33 | echo '
';
34 | echo 'IP assigned (Y-m-d H:i:s): ' . date('Y-m-d H:i:s',$phpIp2Country->getInfo(IP_ASSIGNED_UNIXTIME));
35 | echo '
';
36 | echo 'IP country RIR (Regional Internet Registry) representation: ' .$phpIp2Country->getInfo(IP_COUNTRY_ISO);
37 | echo '
';
38 | echo 'IP Country Abbreviation: ' . $phpIp2Country->getInfo(IP_COUNTRY_CODE);
39 | echo '
';
40 | echo 'IP country name: ' . $phpIp2Country->getInfo(IP_COUNTRY_NAME);
41 | echo '
';
42 | echo 'IP range: ' . nl2br(var_export($phpIp2Country->getInfo(IP_RANGE),true));
43 | echo '
';
44 | echo 'IP range numerical: ' . nl2br(var_export($phpIp2Country->getInfo(IP_RANGE_NUMERICAL),true));
45 | echo '
';
46 | echo 'full IP informations array: ' . nl2br(var_export($phpIp2Country->getInfo(IP_INFO),true));
47 | }
48 | ?>
49 |
50 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/phpip2country.class.php:
--------------------------------------------------------------------------------
1 | chceckIpAddr($ip)){
33 | die('Bad IP address! Should be in xxx.xxx.xxx.xxx format!');
34 | }else{
35 | $this->ip = $ip;
36 | }
37 |
38 | if(!is_array($dbConfig)){
39 | die('Error! Database configuration not set! #1');
40 | }else{
41 | $this->dbConfig = $dbConfig;
42 | }
43 |
44 | $this->dbConnect();
45 |
46 | $this->ipArr = $this->getIpArr();
47 | $this->ipValue = $this->getIpValue();
48 |
49 | $this->ipInfoArr = $this->dbGetRow($this->getIpSelectSQL());
50 |
51 | if(!$this->ipInfoArr){
52 | die ('Error during reciving informations about IP address!');
53 | }else{
54 | $this->ipInfoArr['IP_STR'] = $this->ip;
55 | $this->ipInfoArr['IP_VALUE'] = $this->ipValue;
56 | $this->ipInfoArr['IP_FROM_STR'] = $this->getIpFromValue($this->ipInfoArr['IP_FROM']);
57 | $this->ipInfoArr['IP_TO_STR'] = $this->getIpFromValue($this->ipInfoArr['IP_TO']);
58 | }
59 | }
60 |
61 | function __destruct(){
62 | if($this->db)
63 | mysql_close($this->db);
64 | }
65 |
66 | /**
67 | * IP address
68 | *
69 | * @var string
70 | */
71 | public $ip = '';
72 |
73 | /**
74 | * Numerical representation of IP address
75 | * Example: (from Right to Left)
76 | * 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
77 | * is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
78 | * @var integer
79 | */
80 | private $ipValue = NULL;
81 |
82 | /**
83 | * database conection configuration
84 | * feel free to replece our db conection and use Your favorite database abstraction layer (ie. ADOdb)
85 | *
86 | * @var array
87 | */
88 | public $dbConfig = array();
89 |
90 | /**
91 | * database conection object
92 | * feel free to replece our db conection and use Your favorite database abstraction layer (ie. ADOdb)
93 | *
94 | * @var object
95 | */
96 | public $db = false;
97 |
98 | /**
99 | * IP address in form of array of integer values
100 | *
101 | * @var string
102 | */
103 | private $ipArr = array();
104 |
105 | /**
106 | * IP address information array
107 | *
108 | * @var string
109 | */
110 | private $ipInfoArr = false;
111 |
112 | /**
113 | * returns information about IP adrress
114 | *
115 | * @param integer $mode
116 | * @return mixed
117 | */
118 | public function getInfo($mode=IP_INFO){
119 | if(!in_array($mode,array( IP_STR , IP_VALUE, IP_RANGE_NUMERICAL, IP_RANGE, IP_REGISTRY, IP_ASSIGNED_UNIXTIME , IP_COUNTRY_ISO, IP_COUNTRY_CODE, IP_COUNTRY_NAME, IP_INFO, ))){
120 | die('Error! Bad getInfo() mode!');
121 | }else switch($mode){
122 | case IP_STR:
123 | return $this->ipInfoArr['IP_STR'];
124 | break;
125 | case IP_VALUE:
126 | return $this->ipInfoArr['IP_VALUE'];
127 | break;
128 | case IP_RANGE_NUMERICAL:
129 | return array(
130 | 'FROM' => $this->ipInfoArr['IP_FROM'],
131 | 'TO' => $this->ipInfoArr['IP_TO']
132 | );
133 | break;
134 | case IP_RANGE:
135 | return array(
136 | 'FROM' => $this->ipInfoArr['IP_FROM_STR'],
137 | 'TO' => $this->ipInfoArr['IP_TO_STR']
138 | );
139 | break;
140 | case IP_REGISTRY:
141 | return $this->ipInfoArr['REGISTRY'];
142 | break;
143 | case IP_ASSIGNED_UNIXTIME:
144 | return $this->ipInfoArr['ASSIGNED'];
145 | break;
146 | case IP_COUNTRY_ISO:
147 | return $this->ipInfoArr['CTRY'];
148 | break;
149 | case IP_COUNTRY_CODE:
150 | return $this->ipInfoArr['CNTRY'];
151 | break;
152 | case IP_COUNTRY_NAME:
153 | return $this->ipInfoArr['COUNTRY'];
154 | break;
155 | case IP_INFO:
156 | default:
157 | return $this->ipInfoArr;
158 | break;
159 | }
160 | }
161 |
162 | /**
163 | * validate IP address
164 | *
165 | * @param string $ip
166 | * @return boolean
167 | */
168 | private function chceckIpAddr($ip=''){
169 | return preg_match('/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/i',$ip);
170 | }
171 |
172 | /**
173 | * returns IP address in array of integer values
174 | *
175 | * @return array
176 | */
177 | private function getIpArr(){
178 | $vars = explode('.',$this->ip);
179 | return array(
180 | intval($vars[0]),
181 | intval($vars[1]),
182 | intval($vars[2]),
183 | intval($vars[3])
184 | );
185 | }
186 |
187 | /**
188 | * returns numerical representation of IP address.
189 | * Example: (from Right to Left)
190 | * 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
191 | * is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
192 | *
193 | * @return integer
194 | */
195 | private function getIpValue(){
196 | return $this->ipArr[3] + ( $this->ipArr[2] * 256 ) + ( $this->ipArr[1] * 256 * 256 ) + ( $this->ipArr[0] * 256 * 256 * 256 );
197 | }
198 |
199 | /**
200 | * returns IP numer from numerical representation.
201 | * Example: (from Right to Left)
202 | * 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
203 | * is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
204 | *
205 | * @param integer $value
206 | * @param boolean $returnAsStr
207 | * @return mixed
208 | */
209 | private function getIpFromValue($value=0,$returnAsStr=true){
210 | $ip[0] = floor( intval($value) / (256*256*256) );
211 | $ip[1] = floor( ( intval($value) - $ip[0]*256*256*256 ) / (256*256) );
212 | $ip[2] = floor( ( intval($value) -$ip[0]*256*256*256 -$ip[1]*256*256 ) / 256 );
213 | $ip[3] = intval($value) - $ip[0]*256*256*256 - $ip[1]*256*256 - $ip[2]*256;
214 | if($returnAsStr){
215 | return $ip[0].'.'.$ip[1].'.'.$ip[2].'.'.$ip[3];
216 | }else{
217 | return $ip;
218 | }
219 | }
220 |
221 | /**
222 | * returns SQL used to get iformation from ip2country database
223 | *
224 | * @return string
225 | */
226 | private function getIpSelectSQL(){
227 | if(empty($this->dbConfig['tableName'])){
228 | $this->dbConfig['tableName'] = 'ip_to_country'; //setting default mysql port name
229 | echo "phpPp2Country table name not selected! traying default value: '".$this->dbConfig['tableName']."'";
230 | }
231 | return 'SELECT * FROM '.$this->dbConfig['tableName'].' WHERE IP_FROM <= '.$this->ipValue.' AND IP_TO >= '.$this->ipValue;
232 | }
233 |
234 | /**
235 | * connect to database
236 | * feel free to replece our function and use here Your favorite(s) database abstraction layer (ie. ADOdb)
237 | *
238 | * @return object - database conection resource
239 | */
240 | private function dbConnect(){
241 | if(is_array($this->dbConfig)){
242 | if(empty($this->dbConfig['host'])){
243 | $this->dbConfig['host'] = 'localhost'; //setting default mysql port name
244 | echo "Database connection host not selected! traying default value: '".$this->dbConfig['port']."'";
245 | }
246 | if(intval($this->dbConfig['port']==0)){
247 | $this->dbConfig['port'] = 3306; //setting default mysql port name
248 | echo "Database connection port not selected! traying default value: '".$this->dbConfig['port']."'";
249 | }
250 | if(empty($this->dbConfig['dbUserName'])){
251 | $this->dbConfig['dbUserName'] = 'ip_to_country'; //setting default mysql port name
252 | echo "Database connection host not selected! traying default value: '".$this->dbConfig['dbUserName']."'";
253 | }
254 | if(empty($this->dbConfig['dbUserPassword'])){
255 | $this->dbConfig['dbUserPassword'] = 'xxx'; //setting default mysql port name
256 | echo "Database connection host not selected! traying default value: '".$this->dbConfig['dbUserPassword']."'";
257 | }
258 | $this->db = mysql_connect($this->dbConfig['host'].':'.$this->dbConfig['port'], $this->dbConfig['dbUserName'], $this->dbConfig['dbUserPassword']);
259 | if (!$this->db) {
260 | die('Database connection error: ' . mysql_error());
261 | }else{
262 | if(empty($this->dbConfig['dbName'])){
263 | $this->dbConfig['dbName'] = 'ip_to_country'; //setting default mysql port name
264 | echo "Database connection host not selected! traying default value: '".$this->dbConfig['dbName']."'";
265 | }
266 | if( !mysql_select_db( $this->dbConfig['dbName'] , $this->db ) ){
267 | die("Error during selecting database '".$this->dbConfig['dbName']."' : ". mysql_error());
268 | }else{
269 | return true;
270 | }
271 | }
272 | }else{
273 | die('Error! Database configuration not set! #2');
274 | }
275 | }
276 |
277 | /**
278 | * executes given SQL querry and returns one row
279 | * feel free to replece our function and use here Your favorite(s) database abstraction layer (ie. ADOdb)
280 | *
281 | * @param string $sql
282 | * @return array
283 | */
284 | private function dbGetRow($sql){
285 | $result = mysql_query($sql);
286 | if($result){
287 | $row = mysql_fetch_assoc($result);
288 | if($row){
289 | return $row;
290 | }
291 | }
292 | die("Error during database querry:" . mysql_error());
293 | }
294 | }
295 |
--------------------------------------------------------------------------------