anime-backlog-list/php/check_allowed_ip.php

27 lines
611 B
PHP
Raw Normal View History

2025-01-07 12:23:54 +05:00
<?php
$allowedSubnets = [
'192.168.1.0/24',
'10.0.0.0/8',
'172.16.0.0/12',
'77.91.72.48/32',
'93.140.0.0/12',
'94.140.0.0/12'
];
function isAllowedIP($ip, $subnets) {
foreach ($subnets as $subnet) {
list($subnetBase, $bits) = explode('/', $subnet);
$ipLong = ip2long($ip);
$subnetLong = ip2long($subnetBase);
$mask = -1 << (32 - $bits);
$subnetLong &= $mask;
if (($ipLong & $mask) === $subnetLong) {
return true; // Совпадение найдено
}
}
return false; // Нет совпадений
}