php 如何通过PHP检索访问者的ISP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/855967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I retrieve the visitor's ISP through PHP?
提问by
How do I find out the ISP provider of a person viewing a PHP page?
如何找出查看 PHP 页面的人的 ISP 提供商?
Is it possible to use PHP to track or reveal it?
是否可以使用 PHP 来跟踪或显示它?
If I use something like the following:
如果我使用以下内容:
gethostbyaddr($_SERVER['REMOTE_ADDR']);
it returns my IP address, not my host name or ISP.
它返回我的 IP 地址,而不是我的主机名或 ISP。
回答by Nicholas Flynt
This seems to be what you're looking for, it will attempt to return the full hostname if possible:
这似乎是您要查找的内容,如果可能,它将尝试返回完整的主机名:
回答by Rajesh
EDIT: This method no longer workssince the website it hits now blocks automatic queries (and previously this method violated the website's terms of use). There are several other good [legal!] answers below (including my alternativethis this one.)
编辑:此方法不再有效,因为它现在访问的网站会阻止自动查询(以前此方法违反了网站的使用条款)。下面还有其他几个很好的 [法律!] 答案(包括我的替代方案这个。)
You can get all those things from the following PHP codings.,
您可以从以下 PHP 编码中获得所有这些内容。,
<?php
$ip=$_SERVER['REMOTE_ADDR'];
$url=file_get_contents("http://whatismyipaddress.com/ip/$ip");
preg_match_all('/<th>(.*?)<\/th><td>(.*?)<\/td>/s',$url,$output,PREG_SET_ORDER);
$isp=$output[1][2];
$city=$output[9][2];
$state=$output[8][2];
$zipcode=$output[12][2];
$country=$output[7][2];
?>
<body>
<table align="center">
<tr><td>ISP :</td><td><?php echo $isp;?></td></tr>
<tr><td>City :</td><td><?php echo $city;?></td></tr>
<tr><td>State :</td><td><?php echo $state;?></td></tr>
<tr><td>Zipcode :</td><td><?php echo $zipcode;?></td></tr>
<tr><td>Country :</td><td><?php echo $country;?></td></tr>
</table>
</body>
回答by James Baker
There is nothing in the HTTP headers to indicate which ISPa user is coming from, so the answer is no, there is no PHP builtin function which will tell you this. You'd have to use some sort of service or library which maps IPs to networks/ISPs.
HTTP 标头中没有任何内容表明用户来自哪个ISP,因此答案是否定的,没有 PHP 内置函数会告诉您这一点。您必须使用某种将 IP 映射到网络/ISP 的服务或库。
回答by Jacob Evans
Why not use ARIN's REST API.
为什么不使用 ARIN 的 REST API。
<?php
// get IP Address
$ip=$_SERVER['REMOTE_ADDR'];
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, 'http://whois.arin.net/rest/ip/' . $ip);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
// execute
$returnValue = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$result = json_decode($returnValue);
echo <<<END
<pre>
Handle: {$result->net->handle->{'$'}}
Ref: {$result->net->ref->{'$'}}
Name: {$result->net->name->{'$'}}
echo "OrgRef: {$result->net->orgRef->{'@name'}}";
</pre>
END;
// eof
回答by Max Web Tales
Sometimes fields change, so this is the improvement of the above post.
有时字段会发生变化,因此这是对上述帖子的改进。
<body>
<table align="center">
<?
$ip=$_SERVER['REMOTE_ADDR'];
$url=file_get_contents("http://whatismyipaddress.com/ip/$ip");
preg_match_all('/<th>(.*?)<\/th><td>(.*?)<\/td>/s',$url,$output,PREG_SET_ORDER);
for ($q=0; $q < 25; $q++) {
if ($output[$q][1]) {
if (!stripos($output[$q][2],"Blacklist")) {
echo "<tr><td>".$output[$q][1]."</td><td>".$output[$q][2]."</td></tr>";
}
}
}
?>
</table>
</body>
回答by Eddy
go to http://whatismyip.com
this will give you your internet address. Plug that address into the database at http://arin.net/whois
这会给你你的互联网地址。将该地址插入到http://arin.net/whois的数据库中
回答by schmilblick
GeoIP will help you with this: http://www.maxmind.com/app/locate_my_ip
GeoIP 将帮助您解决这个问题:http: //www.maxmind.com/app/locate_my_ip
There is a php library for accessing geoip data: http://www.maxmind.com/app/php
有一个用于访问 geoip 数据的 php 库:http: //www.maxmind.com/app/php
Attention though, you need to put the geoip db on your machine in order to make it work, all instructions are there :)
但是请注意,您需要将 geoip db 放在您的机器上才能使其工作,所有说明都在那里:)
回答by jwenting
You can't rely on either the IP address or the host name to know the ISP someone is using. In fact he may not use an ISP at all, or he might be logged in through a VPN connection to his place of employment, from there using another VPN or remote desktop to a hosting service halfway around the world, and connect to you from that. The ip address you'd get would be the one from either that last remote machine or from some firewall that machine is sitting behind which might be somewhere else again.
您不能依靠 IP 地址或主机名来了解某人正在使用的 ISP。事实上,他可能根本不使用 ISP,或者他可能通过 VPN 连接登录到他的工作地点,从那里使用另一个 VPN 或远程桌面到世界各地的托管服务,并从那里连接到你. 您获得的 IP 地址将来自最后一台远程机器或来自该机器所在的某些防火墙的 IP 地址,该防火墙可能再次位于其他地方。
回答by navjot singh
If all these answers are not useful then you can try API way.
如果所有这些答案都没有用,那么您可以尝试 API 方式。
1.http://extreme-ip-lookup.com/json/[IPADDRESS HERE]
1. http://extreme-ip-lookup.com/json/[IP地址在这里]
EXAMPLE:http://extreme-ip-lookup.com/json/106.192.146.13
示例:http : //extreme-ip-lookup.com/json/106.192.146.13
2.http://ip-api.com/json/[IPADDRESS HERE]
2. http://ip-api.com/json/[IP地址在这里]
EXAMPLE:http://ip-api.com/json/113.14.168.85
示例:http : //ip-api.com/json/113.14.168.85
once it works for you don't forget to convert JSON into PHP.
一旦它对您有用,请不要忘记将 JSON 转换为 PHP。
回答by navjot singh
I've attempted to correct Ram Kumar's answer but whenever I would edit their post I would be temporarily banned and my changes were ignored. (As to why, I don't know, It was my first and only edit that I've ever made on this website.)
我试图更正 Ram Kumar 的回答,但每当我编辑他们的帖子时,我都会被暂时禁止,我的更改被忽略。(至于为什么,我不知道,这是我在本网站上进行的第一次也是唯一一次编辑。)
Since his post, his code does not work anymore due to website changes and the Administrator implementing basic bot checks (checking the headers):
自从他发帖后,由于网站更改和管理员实施基本机器人检查(检查标题),他的代码不再起作用:
<?php
$IP = $_SERVER['REMOTE_ADDR'];
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0';
$Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$Accept_Language = 'en-US,en;q=0.5';
$Referer = 'http://whatismyipaddress.com/';
$Connection = 'keep-alive';
$HTML = file_get_contents("http://whatismyipaddress.com/ip/$IP", false, stream_context_create(array('http' => array('method' => 'GET', 'header' => "User-Agent: $User_Agent\r\nAccept: $Accept\r\nAccept-Language: $Accept_Language\r\nReferer: $Referer\r\nConnection: $Connection\r\n\r\n"))));
preg_match_all('/<th>(.*?)<\/th><td>(.*?)<\/td>/s', $HTML, $Matches, PREG_SET_ORDER);
$ISP = $Matches[3][2];
$City = $Matches[11][2];
$State = $Matches[10][2];
$ZIP = $Matches[15][2];
$Country = $Matches[9][2];
?>
<body>
<table align="center">
<tr><td>ISP :</td><td><?php echo $ISP;?></td></tr>
<tr><td>City :</td><td><?php echo $City;?></td></tr>
<tr><td>State :</td><td><?php echo $State;?></td></tr>
<tr><td>Zipcode :</td><td><?php echo $ZIP;?></td></tr>
<tr><td>Country :</td><td><?php echo $Country;?></td></tr>
</table>
</body>
Note that just supplying a user-agent would probably suffice and the additional headers are most likely not required, I just added them to make the request look more authentic.
请注意,仅提供用户代理可能就足够了,并且很可能不需要额外的标头,我只是添加了它们以使请求看起来更真实。

