使用PHP和MySQL数据库批量查找IPv6地址

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PHP和MySQL数据库批量查找IPv6地址相关的知识,希望对你有一定的参考价值。

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.
  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. ?>

以上是关于使用PHP和MySQL数据库批量查找IPv6地址的主要内容,如果未能解决你的问题,请参考以下文章

解决iis+php+mysql访问速度慢的方法

如何批量验证字符串是有效的 IPv4 或 IPv6 地址?

批量生成IPv6 subnet地址

批量生成IPv6 host地址

批量生成IPv6 range地址

使用 PHP 扩展 IPv6 地址的快速方法