Получение информации об IP-адресе

Класс получает информацию об IP-адресе: страна, город и т.п. с сервера ip-adress.com. Можно делать не более 20 запросов с одного IP-адреса. В классе предусмотрена возможность использования прокси, чтобы обойти это ограничение.

ipinfo.class.php

/*
+--------------------------------------------------------------------------
|  IpInfo
|  ========================================
|  by Seregwethrin
|  Mail To: seregwethrin@gmail.com (send me bugs if found)
|  ========================================
|
+---------------------------------------------------------------------------
|
|  > Finds city, country, isp, state, organization by ip address
|  > Script written by Seregwethrin
|  > Revision: 1.1
|  > Date: 2007-02-10
|  > Date started: 2007-01-20
|
+--------------------------------------------------------------------------
|
|  > This is a test script. I don't know anything about copyrights of ip-adress.com.
|  > Get your permissions at ip-adress.com before using this script!
|  > php_curl.dll MUST BE LOADED!
|
+--------------------------------------------------------------------------
*/

class ipinfo
{
  var $remote_addr;
  var $address;
  var $country;
  var $state;
  var $city;
  var $isp;
  var $organization;
  var $result;

  function ipinfo()
  {
    //Save remote addr as class variable.
    $this->remote_addr = $_SERVER["REMOTE_ADDR"];

    if (!file_exists("ip_info_proxy.txt"))
      die("Please upload ip_info_proxy.txt file and chmod it to 0777");

    if (!is_writable("ip_info_proxy.txt"))
    {
      @chmod("ip_info_proxy.txt", 0777);
      if (!is_writable("ip_info_proxy.txt"))
        die("Please CHMOD ip_info_proxy.txt to 0777");
    }

    //Load curl extension if not loaded.
    if (!extension_loaded('curl'))
    {
      if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
        if (!dl('php_curl.dll'))
          die("Please read readme.txt and load curl extension");
      else
        if (!dl('curl.so'))
          die("Please read readme.txt and load curl extension");
    }
  }

  //Get datas between begin and end html codes.
  function between($beg, $end, $str)
  {
    $a = explode($beg, $str, 2);
    $b = explode($end, $a[1]);
    return $b[0];
  }

  //Trim the codes between < and >, so trim html tags.
  function trim_html($str)
  {
    return trim(preg_replace("/<([^>]+)>/", "", $str));
  }

  //Choose a proxy for connection
  function get_proxy()
  {
    //Write more proxies. Each proxy gives you 20 query. 127.0.0.1:80's mean is non-proxy.
    //Write as ip:port
    $proxy = array ("127.0.0.1:80", "202.103.178.162:8080", "62.150.130.26:80", "195.175.37.6:8080");

    $txt = file_get_contents("ip_info_proxy.txt");
    $txtx = [];

    //Explode (generate array) the ip_info_proxy.txt datas
    foreach (explode("\n", $txt) as $k=>$v)
    {
      list($txtx[$k]["proxy"], $txtx[$k]["date"], $txtx[$k]["used"]) = explode("-", $v);
    }

    //If proxy is not used for 24 hours, reset it.
    foreach ($txtx as $k=>$v)
    {
      if ($v["date"] != date("d.m.Y"))
      {
        $txtx[$k]["date"] = date("d.m.Y");
        $txtx[$k]["used"] = 0;
      }

    }

    //Choose a proxy for connection, and increase it's usage by 1.
    foreach ($txtx as $k=>$v)
    {
      if ($v["used"] < 20)
      {
        $this->proxy = $v["proxy"];
        $txtx[$k]["used"]++;
        break;
      }
    }

    //Generate write var for writing ip_info_proxy.txt
    $write = "";
    foreach ($txtx as $v)
    {
      if (in_array($v["proxy"], $proxy))
        $write .= $v["proxy"]."-".$v["date"]."-".$v["used"]."\n";
    }

    foreach ($proxy as $v)
    {
      if(!preg_match('/'.$v.'/', $write))
        $write .= $v."-".date("d.m.Y")."-0\n";
    }

    //Write it
    $fp = fopen("ip_info_proxy.txt", "w+");
    fwrite($fp, trim($write));
    fclose($fp);
  }

  function check($ip = false, $proxy=false)
  {
    if ($ip == false) //If ip not given, use remote_addr (client's ip)
    $ip = $this->remote_addr;

    if ($proxy != false) //If proxy is given, use it.
      $this->proxy = $proxy;
    else //If proxy is not given, get a proxy
      $this->get_proxy();

    $postfields = "iq=".urlencode($ip)."&submit=".urlencode("lookup any ip");

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.ip-adress.com/index.php');
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($ch, CURLOPT_REFERER, "http://www.ip-adress.com");
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, "60");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
      if ($this->proxy && $this->proxy != "127.0.0.1:80")
      {
        curl_setopt($ch, CURLOPT_PROXY, $this->proxy); //Use the proxy!
        //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
      }
    $html = curl_exec($ch);
    curl_close($ch);

    if($html && $html != "1")
    {
      $this->address = $this->trim_html($this->between("My ip address:</font>","</font>", $html));
      $this->country = $this->trim_html($this->between("IP country:</font>","</font>", $html));
      $this->state = $this->trim_html($this->between("IP state:</font>","</font>", $html));
      $this->city = $this->trim_html($this->between("IP city:</font>","</font>", $html));
      $this->isp = $this->trim_html($this->between("isp:</font>","</font>", $html));
      $this->organization = $this->trim_html($this->between("organization:</font>","</font>", $html));
      $this->result = "ok"; //Success!
    }
    elseif ($html == "1")
      $this->result = "This ip/proxy has reached limit for this day! (20 query)"; //Fail use more proxies
    else
      $this->result = "Can't get the page!"; //Fail :(
  }
}

/*
+--------------------------------------------------------------------------
|  IpInfo Proxy Checker
|  ========================================
|  by Seregwethrin
|  Mail To: seregwethrin@gmail.com (send me bugs if found)
|  ========================================
|
+---------------------------------------------------------------------------
|
|  > Check proxies for ip-adress.com
|  > Script written by Seregwethrin
|  > Revision: 1
|  > Date: 2007-02-10
|  > Date started: 2007-02-10
|
+--------------------------------------------------------------------------
*/
?>
<html>
<head>
<title>Proxy Checker for IPinfo by Seregwethrin</title>
</head>
<body>
<h1 align="center">Proxy Checker for IP-Info</h1>
<h3 align="center">Will check 64.233.183.103 (Google.com) :)</h3>
<p align="center">Use this, before query 20 ips different from yours at
    <a href="http://www.ip-adress.com" target="_blank">ip-adress.com</a><br>
    <small>Because some transparent proxies can't hide your ip but some can.</small></p>
<form id="form1" name="form1" method="post" action="">
 <table width="200" border="1" align="center" cellpadding="5">
  <tr>
   <td align="center">
     <input name="proxy" type="text" id="proxy" size="24"
         onFocus="if (this.value == 'Write Proxy Here as ip:port') this.value = '';"
         onBlur="if (this.value == '') this.value = 'Write Proxy Here as ip:port';"
         value="Write Proxy Here as ip:port" /></td>
  </tr>
  <tr>
   <td align="center"><input name="submit" type="submit" id="submit" value="Check" /></td>
  </tr>
 </table>
</form>
<?php
if (@$_GET["submit"])
{
  include("ipinfo.class.php");
  $ipinfo = new ipinfo;

  $ipinfo->check("64.233.183.103", $_GET["proxy"]);

  if ($ipinfo->result == "ok")
    echo '<center><img src="http://img157.imageshack.us/img157/1179/afftickwd7.gif" border="0" />
    this proxy is usefull.</center>';
  else
    echo '<center><img src="http://img157.imageshack.us/img157/9692/affcrossul4.gif" border="0" />
    Sorry but there\'s an error with this proxy.<br>
    <small><i>Error is:'.$ipinfo->result.'</i></small></center>';

    echo  "<center><br><br>IP Address:".$ipinfo->address;
    echo  "<br>Country:".$ipinfo->country;
    echo  "<br>State:".$ipinfo->state;
    echo  "<br>City:".$ipinfo->city;
    echo  "<br>ISP:".$ipinfo->isp;
    echo  "<br>Organization:".$ipinfo->organization;
    echo  "<br>Result:".$ipinfo->result;
    echo  "<br><b>Proxy:</b>".$_GET["proxy"]."</center>";
}
?>
</body>
</html>

Для работы нужно подготовить файл ip_info_proxy.txt со списком Proxy серверов


.