我有一个记录每个成员的 IP 地址、浏览器和操作系统的系统。
我想以某种方式实现这一点。
无论如何,我可以将用户 IP 发布到他们的网站,提取 ISP 和国家/地区等值,并将它们存储在我的本地 MySQL 数据库中,以便在对某些滥用用户运行查询时快速访问?
IP 到位置并不总是准确的,很容易克服,但这可能会对您有所帮助
如果你真的想从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;
}
?>
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";
?>
也许这会让你朝着正确的方向前进?