Advertisement
hexasoft

IP Address Lookup in Bulk Using PHP and MySQL for IPV6

May 16th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.05 KB | None | 0 0
  1. /****************************************************************************************************************/
  2. /* Description: This snippet is provide simple way to help user on how to lookup IP address in bulk .           */
  3. /*              suggestions for different city locations across the country.                                    */
  4. /*              There are 1 steps in this snippet. For information, please visit IP2Location tutorial page at:  */
  5. /* https://www.ip2location.com/tutorials/lookup-ip-address-in-bulk-using-php-and-mysql-database                 */
  6. /****************************************************************************************************************/
  7. /* You can obtain free IP2Location LITE database for IPv6 at https://lite.ip2location.com                       */
  8.  
  9. <?php
  10.  
  11.     // MYSQL configuration
  12.     $mysql_server = "mysql_server.com";
  13.     $mysql_user_name ="User ID";
  14.     $mysql_user_pass ="password";
  15.  
  16.     // Connect to the database server
  17.     $link = mysqli_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
  18.  
  19.     // Connect to the IP2Location database
  20.     mysqli_select_db($link,"ip2location") or die("Could not select database");
  21.  
  22.  
  23.     //Display for user input
  24.     echo "<html>";
  25.     echo "<head><title>IP Query in Bulk</title></head>";
  26.     echo "<body>
  27.            <span>Upload IP list for validation:</span><br/><br/>
  28.            <form action='' method='post' enctype='multipart/form-data'>
  29.            <input name='uploaded_file' type='file' value='' /><br>
  30.            <input type='submit' name='submit' value='Upload & Process' />
  31.            </form>
  32.        </body>";
  33.     echo "</html>";
  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 = mysqli_query($link,$query) or die("IP2Location Query Failed");
  90.  
  91.  
  92.             while($row = mysqli_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.         // Free recordset and close database connection
  107.         mysqli_free_result($result);
  108.         mysqli_close($link);
  109.     }
  110.  
  111.  
  112.     // Function to convert IP address to IP number (IPv6)
  113.         function Dot2LongIPv6 ($IPaddr) {
  114.             $int = inet_pton($IPaddr);
  115.             $bits = 15;
  116.             $ipv6long = 0;
  117.             while($bits >= 0){
  118.                 $bin = sprintf("%08b", (ord($int[$bits])));
  119.                 if($ipv6long){
  120.                     $ipv6long = $bin . $ipv6long;
  121.                 }
  122.                 else{
  123.                     $ipv6long = $bin;
  124.                 }
  125.                 $bits--;
  126.             }
  127.             $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
  128.             return $ipv6long;
  129.         }
  130.  
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement