IP Address (IPv6) Lookup in Bulk Using PHP and MySQL Database


/ Published in: PHP
Save to your folder(s)

The code below can be used to lookup IP address in bulk in return of geolocation information using PHP programming languages and MySQL database. In this tutorial, we use the IP2Location LITE database to lookup country of origin from the visitor's IP address. Free databases are available for download at IP2Location LITE database.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. //MYSQL configuration
  4. $mysql_server = "mysql_server.com";
  5. $mysql_user_name ="User ID";
  6. $mysql_user_pass ="password";
  7.  
  8. //Connect to database
  9. $link = mysql_connect($mysql_server,$mysql_user_name,$mysql_user_pass) or die("could not connect to MySQL Database");
  10.  
  11. //Check database connection
  12. if(!$link)
  13. {
  14. echo "Database error.";
  15. }
  16. else
  17. {
  18. mysql_select_db("ip2location") or die("could not select database");
  19. echo "Database is Connected .";
  20. echo "<br><br>";
  21. }
  22.  
  23. //Display for user input
  24. echo "";
  25. echo "<title>IP Query in Bulk</title>";
  26. echo "
  27. <span>Upload IP list for validation:</span><br><br>
  28. <form action="" enctype="multipart/form-data" method="post">
  29. <input name="uploaded_file" type="file" value=""><br>
  30. <input name="submit" type="submit" value="Upload & Process">
  31. </form>
  32. ";
  33. echo "";
  34.  
  35.  
  36. //File submitted
  37. if(isset($_POST['submit']))
  38. {
  39. //Check for file error
  40. if($_FILES["uploaded_file"]["error"] > 0)
  41. {
  42. echo "Error :" .$_FILES["uploaded_file"]["error"]. "<br>";
  43. }
  44. else
  45. {
  46. echo "Input File Path :" , realpath(dirname(__FILE__)) ;
  47. echo "<br>";
  48. echo "File Name : " .$_FILES["uploaded_file"]["name"]. "<br>";
  49. echo "Type : " .$_FILES["uploaded_file"]["type"]. "<br>";
  50. echo "Size : " .($_FILES["uploaded_file"]["size"]/ 1024). "KB<br>";
  51. }
  52.  
  53. //Name the output file
  54. if(file_exists( "result.csv"))
  55. {
  56. $duplicatefile = "result.csv";
  57. unlink($duplicatefile);
  58. echo "Duplicate file deleted ! <br>";
  59. }
  60.  
  61. //Check if uploaded file exists
  62. if(file_exists( $_FILES["uploaded_file"]["name"]))
  63. {
  64. echo"Uploaded file already exist, Please make sure that both file's content are same. <br>" ;
  65. }
  66. else
  67. {
  68. move_uploaded_file($_FILES["uploaded_file"]["tmp_name"],
  69. $_FILES["uploaded_file"]["name"]);
  70. }
  71.  
  72. //Open file from its location
  73. $file = $_FILES["uploaded_file"]["name"];
  74. $ipfile = fopen($file,"r") or exit("unable to open file!");
  75.  
  76. //To split up the IP Address and fetch data from server
  77. while(! feof($ipfile))
  78. {
  79. $iplist = stream_get_line($ipfile,100,",");
  80.  
  81. $ipno = Dot2LongIPv6($iplist);
  82. $query = "SELECT * FROM ip2location_db11 WHERE ip_to >= $ipno order by ip_to limit 1 ";
  83.  
  84. if(!$query)
  85. {
  86. echo "Error";
  87. }
  88.  
  89. $result = mysql_query($query) or die("IP2Location Query Failed");
  90.  
  91.  
  92. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  93. {
  94. $current = "\"$iplist\",\"$ipno\",\"{$row['country_code']}\",\"{$row['country_name']}\",\"{$row['region_name']}\",\"{$row['city_name']}\",\"{$row['latitude']}\",\"{$row['longitude']}\",\"{$row['zip_code']}\",\"{$row['time_zone']}\"" ;
  95.  
  96. //Output file to the path you want
  97. $ans = "result.csv";
  98. $fp = fopen($ans,"a") or die("couldn't open $ans for writing");
  99. fwrite($fp,$current) or die ("couldn't write values to file!");
  100. fclose($fp);
  101. }
  102. }
  103. echo "Result File Path : ", realpath($ans);
  104. echo "<br>";
  105.  
  106. mysql_free_result($result); mysql_close($link);
  107. }
  108.  
  109.  
  110. // Function to convert IP address to IP number (IPv6)
  111. function Dot2LongIPv6 ($IPaddr) {
  112. $int = inet_pton($IPaddr);
  113. $bits = 15;
  114. $ipv6long = 0;
  115. while($bits >= 0){
  116. $bin = sprintf("%08b", (ord($int[$bits])));
  117. if($ipv6long){
  118. $ipv6long = $bin . $ipv6long;
  119. }
  120. else{
  121. $ipv6long = $bin;
  122. }
  123. $bits--;
  124. }
  125. $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
  126. return $ipv6long;
  127. }
  128.  
  129. ?>

URL: http://ip2location.com/tutorials/lookup-ip-address-in-bulk-using-php-and-mysql-database

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.