php – 按国家/地区ip范围阻止用户注册
发布时间:2020-12-13 17:25:24 所属栏目:PHP教程 来源:网络整理
导读:我在MYSQL中有下表 CREATE TABLE IF NOT EXISTS `countryip` (`ip_from` bigint(10) unsigned NOT NULL DEFAULT '0',`ip_to` bigint(10) unsigned NOT NULL DEFAULT '0',`country_iso2` varchar(2) COLLATE utf8_bin NOT NULL DEFAULT '',`country_iso3` var
我在MYSQL中有下表
CREATE TABLE IF NOT EXISTS `countryip` ( `ip_from` bigint(10) unsigned NOT NULL DEFAULT '0',`ip_to` bigint(10) unsigned NOT NULL DEFAULT '0',`country_iso2` varchar(2) COLLATE utf8_bin NOT NULL DEFAULT '',`country_iso3` varchar(3) COLLATE utf8_bin NOT NULL DEFAULT '',`country_name` varchar(32) COLLATE utf8_bin NOT NULL,KEY `ip_from` (`ip_from`,`ip_to`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 在这里,我添加了世界上所有国家的整个IP范围. 我想在php中创建一个脚本,该脚本将用户ip与此数据库中的ip范围进行比较,之后回显消息. <?php mysql_select_db("database_name") or die(mysql_error()); $ip = $_SERVER['REMOTE_ADDR']; $query = mysql_query("SELECT `IP` FROM `ban_ip` WHERE `IP` = '$ip'"); // this works only if i have the single static ip in the dbs,but now i have the ip range if (mysql_num_rows($query) > 0) { echo ' <!DOCTYPE html> <html> <head> <script type="text/javascript"> alert("You are not allowed to access this website"); window.location.replace("index.php"); </script> </head> <body> </body> </html>'; } 我该怎么做而不是这个查询 $query = mysql_query("SELECT `column` FROM `table` WHERE `column` = '$ip'"); 为了使用户IP和IP范围之间的比较成为可能. IP FROM列,TO IP列如下所示 解决方法
您可以对指定的表使用以下SQL查询:
SELECT `country_iso2` FROM `countryip` WHERE `ip_from` <= inet_aton('$ip') AND ip_to >= inet_aton('$ip') 这将为您指定的IP地址选择两个字符的国家/地区ISO代码.从这里开始,只需将ISO代码与禁止的国家代码表中的条目进行比较即可. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |