使用本机 PHP 获取 IP 的国家/地区位置

发布于 2024-08-25 18:21:06 字数 578 浏览 6 评论 0原文

在你说这是重复之前请继续阅读,事实并非如此。 (据我所知)

我想从客户端获取 php 中的县代码。

是的,我知道您可以使用外部站点或“geoip_record_by_name”之类的方式来完成此操作,但我不想依赖外部站点,而且我无法为 php 安装“pear”,因为我使用分片 Dreamhost 托管。

我想我可以做这样的事情:

$output = shell_exec('whois '.$ip.' -H | grep country | awk \'{print $2}\'');
echo "<pre>$output</pre>";

但是dreamhost似乎有一个旧版本的whois(4.7.5),所以我在分配IP时遇到这个错误:

Unknown AS number or IP network. Please upgrade this program.

所以除非有人知道如何获取较新版本的二进制文件whois 到 Dreamhost 我卡住了。

或者是否有其他方法可以从加载页面的客户端获取国家/地区代码?

Read on before you say this is a duplicate, it's not. (as far as I could see)

I want to get the county code in php from the client.

Yes I know you can do this using external sites or with the likes of "geoip_record_by_name" but I don't want to be dependent on an external site, and I can't install "pear" for php as im using shard Dreamhost hosting.

I thought I could just do something like this:

$output = shell_exec('whois '.$ip.' -H | grep country | awk \'{print $2}\'');
echo "<pre>$output</pre>";

But dreamhost seems to have an old version of whois (4.7.5), so I get this error on allot of IPs:

Unknown AS number or IP network. Please upgrade this program.

So unless someone knows how to get a binary of a newer version of whois onto dreamhost im stuck.

Or is there another way I could get the country code from the client who is loading the page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

与君绝 2024-09-01 18:21:06

Whois 只是 whois 服务的客户端,因此从技术上讲,您仍然依赖于外部站点。对于失败的查询,您可以尝试回退到另一个站点进行查询,例如 hostip.info,它恰好有一个不错的 API 并且看起来很友好:

http://api.hostip.info/country.php?ip=4.2.2.2

返回

US

祝你好运,

--jed

编辑:@Mint 这是链接到 hostip.info 上的 API:http://www.hostip.info/use.html

Whois is just a client for the whois service, so technically you are still relying on an outside site. For the queries that fail, you could try falling back to another site for the query, such as hostip.info, who happen to have a decent API and seem friendly:

http://api.hostip.info/country.php?ip=4.2.2.2

returns

US

Good luck,

--jed

EDIT: @Mint Here is the link to the API on hostip.info: http://www.hostip.info/use.html

茶底世界 2024-09-01 18:21:06

MaxMind 提供了一个免费的 PHP GeoIP 国家查找类(还有一个免费的国家+城市查找类) 。

您想要的部分是“纯 PHP 模块”下提到的内容。这不需要您安装任何东西,或者依赖它们,也不需要安装任何特殊的 PHP 模块。只需将 GeoIP 数据文件保存在某处,然后使用他们提供的类与其进行交互。

MaxMind provide a free PHP GeoIP country lookup class (there is also a free country+city lookup one).

The bit you want is what is mentioned under "Pure PHP module". This doesn't require you to install anything, or be dependent on them, nor does it need any special PHP modules installed. Just save the GeoIP data file somewhere, then use their provided class to interact with it.

不弃不离 2024-09-01 18:21:06

您可以将 whois 的副本安装到您​​的主目录中并将完整路径传递给 shell_exec 吗?这样您就不受他们的升级计划的约束。

Can you just install a copy of whois into your home directory and pass the full path into shell_exec? That way you're not bound to their upgrade schedule.

汐鸠 2024-09-01 18:21:06

另一种稍微极端的解决方案是:

  1. 下载 MaxMind 的 国家/地区数据库的 CSV 格式版本
  2. 使用脚本从 CSV 中去除不需要的信息,并
  3. ……生成一个标准 PHP 文件,其中包含一个以 IP 地址为键、以国家/地区代码为值的数据结构。
  4. 将生成的文件包含在您常用的项目文件中,您现在就拥有了一个完全内部的 IP =>国家代码查找表。

缺点是,您需要定期从最新版本的数据库重新生成 PHP 文件。而且,总的来说,这是一种非常糟糕的方式,而且性能可能不是最好的:)

An alternative, somewhat extreme solution to your problem would be to:

  1. Download the CSV format version of MaxMind's country database
  2. Strip out the information you don't need from the CSV with a script and ...
  3. ... generate a standard PHP file which contains a data structure containing the IP address as the key and the country code as the value.
  4. Include the resulting file in your usual project files and you now have a completely internal IP => country code lookup table.

The disadvantage is that, regularly, you would need to regenerate the PHP file from the latest version of the database. Also, it's a pretty nasty way of doing it in general and performance might not be the best :)

朕就是辣么酷 2024-09-01 18:21:06

考虑ipcountryphp(我的网站,我的代码,我的荣幸)因为它提供了本地互联网终身免费更新的数据库。它速度快且完全独立,可插入任何 PHP 5.3、SQLite3 及更高版本。非常快的搜索并且没有性能损失。

无耻的自我推销已经够了,让我们认真点吧:

依靠实时查询远程服务来获取访问者国家可以成为站点功能的主要瓶颈取决于查询服务器的响应速度。根据经验,您永远不应该查询外部服务来获取实时站点功能(例如页面加载)。在后台使用 API 固然很棒,但当您需要在渲染页面之前查询每个访问者所在的国家/地区时,您就会面临一个痛苦的世界。 并且请记住,您并不是唯一一个滥用免费服务的人:)

所以对第三方服务的查询会留在后台,而只有依赖于不依赖的本地功能第三方进入用户交互的层。只是我对此有点性能偏执:)

PS我编写的上述脚本也支持 IPv6。

Consider ipcountryphp (my site, my code, my honour) as it provides a local internet-lifetime freely updated database. It's fast and fully self-contained, pluggable into anything PHP 5.3, SQLite3 and beyond. Very fast seeks and no performance penalties.

Enough with shameless self-promotion, let's get serious:

Relying on querying remote services in real-time to get visitor country can become a major bottleneck for your site's functionality depending on the response speed of the queried server. As a rule of thumb you should never query external services for real-time site functionality (like page loading). Using APIs in the background is great but when you need to query the country of each visitor before the page is rendered, you open yourself up to a world of pain. And do keep in mind you're not the only one abusing free services :)

So queries to 3rd-party services stay in the background while only local functionality that relies on no 3rd-party go into the layers there users interact with. Just my slightly performance paranoid take on this :)

PS: Above mentioned script I wrote has IPv6 support too.

苦妄 2024-09-01 18:21:06

这是一个带有我刚刚使用的脚本的网站。唯一的问题是,您可能时不时地需要自己重新生成 IP...这可能很痛苦,这也是为什么每个人都告诉您使用外部 API。但对我来说这不是解决方案,因为我一次拉了大约 50 个 IP,这意味着我可能会被禁止。所以解决方案是使用我自己的脚本或保存到数据库,但我再次从外部站点提取图像。无论如何,这是我找到脚本的网站:

http://coding-talk。 com/f29/country-flag-script-8882/

Here is a site with a script i just used. The only problem is that you would probably every now and then need to regenerate IPs by yourself... which might be pain and tahts why everyone is telling you to use external API. But for me that wasnt solution as i was pulling like 50 IPs at once, which means i would probably get banned. So solution was to use my own script or to do saves to DB, but i was again pulling images from external sites. Anyway here is the site i found script on:

http://coding-talk.com/f29/country-flag-script-8882/

独闯女儿国 2024-09-01 18:21:06

这也是其中之一。只需将 IP 更改为变量即可:

http://api.codehelper.io/ips/?回调=codehelper_ip_callback&ip=143.3.87.193

here is also one of them. just change the IP to the variable:

http://api.codehelper.io/ips/?callback=codehelper_ip_callback&ip=143.3.87.193

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文