简体   繁体   English

PHP计算子网ip2long的长整数范围

[英]PHP Calculate Long Integer Range of Subnet ip2long

I am trying to determine whether a website visitor came from an EU country, and if so show them a cookie consent banner per their law.我试图确定网站访问者是否来自欧盟国家,如果是,则根据他们的法律向他们展示 cookie 同意横幅。

I downloaded a table listing IPv4 of all EU countries (ie: 1.178.224.0/19) and uploaded it to mySQL.我下载了一个列出所有欧盟国家 IPv4 的表格(即:1.178.224.0/19)并将其上传到 mySQL。 There are 107,000 rows in the table.表中有 107,000 行。 I can get the users IP using $_SERVER['REMOTE_ADDR'].我可以使用 $_SERVER['REMOTE_ADDR'] 获取用户 IP。 I'm looking for the most simplified and quickest approach to check using PHP if the user's ip address falls in the range.如果用户的 IP 地址在该范围内,我正在寻找使用 PHP 检查的最简单和最快捷的方法。

One idea was to create 2 columns in a table with the starting and ending long integers of each IPv4.一个想法是在表中创建 2 列,其中包含每个 IPv4 的起始和结束长整数。 The website would change the users IP address to a long integer using ip2long() and then see if it falls within the range of the 2 columns of any IPv4.该网站将使用 ip2long() 将用户 IP 地址更改为长整数,然后查看它是否在任何 IPv4 的 2 列范围内。 Not sure if this is possible.不确定这是否可能。 If that's a good idea, how do I get the starting and ending long integer of an IPv4 address?如果这是个好主意,我如何获得 IPv4 地址的起始和结束长整数?

I found several solutions to see if an IP falls within the range (ie: 1.178.224.0/19) listed in my table, but with 107,000 rows this would probably take a while to process.我找到了几种解决方案来查看 IP 是否在我的表中列出的范围内(即:1.178.224.0/19),但是对于 107,000 行,这可能需要一段时间来处理。 I'm looking for a solution that runs quick so not to slow down every user who's not in an EU country.我正在寻找一种运行速度快的解决方案,以免减慢不在欧盟国家/地区的每个用户的速度。

Instead of trying to implement geolocation yourself, you could use a free online service for that.您可以使用免费的在线服务,而不是自己尝试实施地理定位。 You can implement this quite quickly.您可以非常快速地实现这一点。 Take for instance this service:以这项服务为例:

http://geolocation-db.com/documentation http://geolocation-db.com/documentation

I would use Javascript not PHP, and only when an user is in an EU country you pop up the cookie consent banner.我会使用 Javascript 而不是 PHP,并且只有当用户在欧盟国家/地区时,您才会弹出 cookie 同意横幅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM