Advertisement
Guest User

Julienl

a guest
Jul 30th, 2011
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author Julienl
  4.  * @version 0.2
  5.  * Insert ip and country in database
  6.  */
  7.  
  8. //Database information
  9. $hote='localhost';
  10. $port='3306';
  11. $db='database';
  12. $user='user';
  13. $password='password';
  14. //////////////////////
  15.  
  16. $c = mysql_connect($hote, $user, $password) or die ("Impossible to connect : " . mysql_error());
  17. mysql_select_db($db) or die("Impossible to use database");
  18.  
  19.  
  20. function execQuery($query)
  21. {
  22.   $result = mysql_query($query);
  23.  
  24.   if(!$result)
  25.   {
  26.     $message  = mysql_error();
  27.     die($message);
  28.   }
  29. }
  30.  
  31. if ($argc != 2)
  32. {
  33.   echo "arguments are missing\n";
  34.   exit(0);
  35. }
  36.  
  37. $ip = $argv[1];
  38.  
  39. $sql = "SELECT COUNT(*) FROM ip WHERE ip = '$ip';";
  40. $res = mysql_query($sql);
  41. $row = mysql_fetch_row($res);
  42.  
  43. if($row[0] == 0)
  44. {
  45.  $country = exec("whois ". $ip ." | grep country | awk -F \" \" '{print $2}\'");
  46.  
  47.  if(!isset($country))
  48.  {
  49.    $country = "Unknow";
  50.  }
  51.  
  52.  $sql = "INSERT INTO ip (ip,country) VALUES ('$ip','$country');";
  53.  
  54.  execQuery($sql);
  55. }
  56.  
  57. mysql_close();
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement