Advertisement
hexasoft

IP Address Lookup in Bulk Using PHP and MySQL for IPV4

May 16th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 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 IPv4 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.     //Display for user input
  23.     echo "<html>";
  24.     echo "<head><title>IP Query in Bulk</title></head>";
  25.     echo "<body>
  26.            <span>Upload IP list for validation:</span><br/><br/>
  27.            <form action='' method='post' enctype='multipart/form-data'>
  28.            <input name='uploaded_file' type='file' value='' /><br>
  29.            <input type='submit' name='submit' value='Upload & Process' />
  30.            </form>
  31.        </body>";
  32.     echo "</html>";
  33.  
  34.  
  35.     //File submitted
  36.     if(isset($_POST['submit']))
  37.     {
  38.         //Check for file error
  39.         if($_FILES["uploaded_file"]["error"] > 0)
  40.         {
  41.             echo "Error :" .$_FILES["uploaded_file"]["error"]. "<br>";
  42.         }
  43.         else
  44.         {
  45.             echo "Input File Path :" , realpath(dirname(__FILE__))  ;
  46.             echo "<br>";
  47.             echo "File Name : " .$_FILES["uploaded_file"]["name"]. "<br>";
  48.             echo "Type : " .$_FILES["uploaded_file"]["type"]. "<br>";
  49.             echo "Size : " .($_FILES["uploaded_file"]["size"]/ 1024). "KB<br>";
  50.         }
  51.  
  52.         //Name the output file
  53.         if(file_exists( "result.csv"))
  54.         {
  55.             $duplicatefile = "result.csv";
  56.             unlink($duplicatefile);
  57.             echo "Duplicate file deleted ! <br>";
  58.         }
  59.  
  60.         //Check if uploaded file exists
  61.         if(file_exists( $_FILES["uploaded_file"]["name"]))
  62.         {
  63.             echo"Uploaded file already exist, Please make sure that both file's content are same. <br>"   ;
  64.         }
  65.         else
  66.         {
  67.             move_uploaded_file($_FILES["uploaded_file"]["tmp_name"],
  68.              $_FILES["uploaded_file"]["name"]);
  69.         }
  70.  
  71.         //Open file from its location
  72.         $file =  $_FILES["uploaded_file"]["name"];
  73.         $ipfile = fopen($file,"r") or exit("unable to open file!");
  74.  
  75.         //To split up the IP Address and fetch data from server
  76.         while(! feof($ipfile))
  77.         {
  78.             $iplist = stream_get_line($ipfile,18,",");
  79.  
  80.             $ipno = Dot2LongIP($iplist);
  81.             $query = "SELECT * FROM ip2location_db11 WHERE ip_to >= $ipno order by ip_to limit 1 ";
  82.  
  83.             if(!$query)
  84.             {
  85.                 echo "Error";
  86.             }
  87.  
  88.             $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
  89.  
  90.  
  91.             while($row = mysqli_fetch_array($result,MYSQL_ASSOC))
  92.                 {
  93.                     $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']}\"" ;
  94.  
  95.                     //Output file to the path you want
  96.                         $ans = "result.csv";
  97.                         $fp = fopen($ans,"a") or die("couldn't open $ans for writing");
  98.                         fwrite($fp,$current) or die ("couldn't write values to file!");
  99.                         fclose($fp);
  100.                 }
  101.         }
  102.                 echo "Result File Path : ", realpath($ans);
  103.                 echo "<br>";
  104.  
  105.         // Free recordset and close database connection
  106.         mysqli_free_result($result);
  107.         mysqli_close($link);
  108.     }
  109.  
  110.  
  111.     // Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
  112.     function Dot2LongIP ($IPaddr)
  113.     {
  114.         if ($IPaddr == "")
  115.         {
  116.         return 0;
  117.         }
  118.         else {
  119.         $ip = explode(".", $IPaddr);
  120.         return ($ip[3] + $ip[2] * 256 + $ip[1] * 256 * 256 + $ip[0] * 256 * 256 * 256);
  121.         }
  122.     }
  123.  
  124. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement