To be frank, building a PHP application that permits you to determine from which country a user is accessing a web page according to his/her IP address is a straightforward process that can be tackled with minor efforts. Basically, this procedure is reduced to performing two simple steps: first, capturing the user IP address and second, determining via the "iptocountry" MySQL table which country is tied to that particular address. However, to clarify this concept a bit further, I coded a sample PHP script (shown below) which accomplishes all the tasks mentioned above. Here it is: try{ // connect to MySQL server if(!$db=mysql_connect('host','user','password')){ throw new Exception ('Error connecting to the server.'); } // select database if(!mysql_select_db('alejandro',$db)){ throw new Exception ('Error selecting database.'); } // get user IP address $ip=sprintf('%u',ip2long($_SERVER['REMOTE_ADDR'])); // Map IP Address to country using 'iptocountry' database if(!$result=mysql_query("SELECT country FROM iptocountry WHERE lower_bound <=$ip AND upper_bound >= $ip LIMIT 1")){ throw new Exception ('Unable to map IP Address to country.'); } // get row from database table $row=mysql_fetch_array($result); // display country which user is accessing from echo 'Welcome dear visitor, you are accessing our web site from : '.$row['country']; } catch(Exception $e){ echo $e->getMessage(); exit(); } See how simple it is to determine from which country a visitor is accessing a web page by means of the "iptocountry" MySQL table? I guess you do! But, to dissipate any doubts, please take a close look at the above code sample. As you can see, it first connects to MySQL and selects a fictional database, and then it gets the IP address of the user currently visiting the web page. Finally, it determines the country that corresponds to that specific address. Naturally, this last step is performed by querying the "iptocountry" MySQL table to find out to which range of addresses the user's IP address belongs. In this case, the values stored on the "lower_bound" and "upper_bound" fields are utilized for performing this comparison and for mapping the IP address to its originating country. That's all. So, suppose that I am the one who is accessing the previous script. It would produce the following output: 'Welcome dear visitor, you are accessing our web site from : ARGENTINA Pretty neat, right? At this point, you hopefully realized how easy it is to build an IP-to-country program with PHP using a procedural approach. As you might have guessed, however, it's also feasible to develop a similar application by means of the object-oriented paradigm. Therefore, the last section of this tutorial will be aimed at illustrating how to create a lookup program by using a basic MySQL abstraction class. The complete details regarding the definition of this class will be revealed in the following section. Please click on the link that appears below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|