从网页发布和请求


Post and request from webpages

我有一个系统,记录每个成员的IP地址、浏览器和操作系统。

我想以某种方式实现这一点。

我是否可以将用户IP发布到他们的网站,提取ISP和国家/地区等值,并将其存储在我的本地MySQL数据库中,以便在对某些滥用权限的用户运行查询时快速访问?

IP到位置并不总是准确的,而且可以很容易地克服,但这可能会帮助您

PHP对Maxmind的GeoIP服务有本机支持。有关详细信息,请参阅此PECL扩展。

查看此网站:http://ipinfodb.com/ip_location_api.php

他们提供API将ip地址传递给服务,以返回XML/JSON中的地理位置,然后可以在PHP脚本上对其进行解析。

如何使用它的示例如下:

<?php
include('ip2locationlite.class.php');
//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey('<your_api_key>');
//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();
//Getting the result
echo "<p>'n";
echo "<strong>First result</strong><br />'n";
if (!empty($locations) && is_array($locations)) {
  foreach ($locations as $field => $val) {
    echo $field . ' : ' . $val . "<br />'n";
  }
}
echo "</p>'n";
//Show errors
echo "<p>'n";
echo "<strong>Dump of all errors</strong><br />'n";
if (!empty($errors) && is_array($errors)) {
  foreach ($errors as $error) {
    echo var_dump($error) . "<br /><br />'n";
  }
} else {
  echo "No errors" . "<br />'n";
}
echo "</p>'n";
?>

也许这会让你朝着正确的方向前进?

如果您真的想从http://www.iplocation.net/,则这里是一个快速脏function。但要使用此函数,您需要下载并包含PHP Simple HTML DOM Parser

这是代码

<?php
  require_once( "path to simplehtmldom.php" );
  $ip_info  = ip_info( "223.196.190.40", 1 );
  print_r( $ip_info );
  /**
   * It will output...
    Array
    (
      [IP Address] => 223.196.190.40
      [Country] => India
      [Region] => Maharashtra
      [City] => Pune
      [ISP] => Idea Isp Subscriber Ip Pool
    )
  **/

  /**
   * ip_info()
   * @param $ip - IP address you want to fetch data from
   * @param $provider IP provider ( 1 = IP2Location, 2 = IPligence, 3 = IP Address Labs, 4 = MaxMind )
   * @return array
   */
  function ip_info( $ip = "127.0.0.1", $provider = 1 ) {
    $indx = array(
      1 =>  10,
      2 =>  11,
      3 =>  12,
      4 =>  13
    );
    $data = array();
    $url  = "http://www.iplocation.net/index.php";
    $ch   = curl_init();
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_FRESH_CONNECT, true );
    curl_setopt( $ch, CURLOPT_FORBID_REUSE, true );
    curl_setopt( $ch, CURLOPT_HEADER, false );
    curl_setopt( $ch, CURLOPT_NOBODY, false );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
    curl_setopt( $ch, CURLOPT_BINARYTRANSFER, false );
    curl_setopt( $ch, CURLOPT_REFERER, $url );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, "query=".urlencode( $ip )."&submit=Query" );
    $response = curl_exec( $ch );
    $html = str_get_html( $response );
    if ( $table = $html->find( "table", $indx[$provider] ) ) {
      if ( $tr1 = $table->find( "tr", 1 ) ) {
        if ( $headers = $tr1->find( "td" ) ) {
          foreach( $headers as $header ) {
            $data[trim( $header->innertext )] = null;
          }
        }
      }
      if ( $tr2 = $table->find( "tr", 3 ) ) {
        reset( $data );
        if ( $values = $tr2->find( "td" ) ) {
          foreach( $values as $value ) {
            $data[key( $data )] = trim( $value->plaintext );
            next( $data );
          }
        }
      }
    }
    unset( $html, $table, $tr1, $tr2, $headers, $values );
    return $data;
  }
?>