Lo siento, soy un novato, pero estoy tratando de usar el script que encontré aquí:
Solo permitir usuarios en la página si se aprueba la dirección IP
$allowedIps = ['xxxx', 'xxxx']; $userIp = $_SERVER['REMOTE_ADDR']; if (!in_array($userIp, $allowedIps)) { exit('Unauthorized'); }
Pero en cambio, quiero cargar miles de rangos de ip desde un archivo .txt
así: (No sé la función correcta)
$allowedIps = ['www.example.com/ip_list.txt'];
lista ip_list.txt:
xx.xxx.xxx.xx/30 xx.xxx.xxx.xx/78 xx.xxx.xxx.xx/59
Este script obtendrá la lista de IP del archivo, la dividirá en líneas, luego convertirá la notación CIDR en una matriz de IP y las fusionará todas juntas. Luego se realiza una verificación y se proporciona un código HTTP 403 si la dirección remota no está en la lista de IP aceptables.
<?php // Function courtesy of the following answer // https://stackoverflow.com/questions/4931721/getting-list-ips-from-cidr-notation-in-php function cidrToRange($cidr) { $range = array(); $cidr = explode('/', $cidr); $range[0] = long2ip((ip2long($cidr[0])) & ((-1 << (32 - (int)$cidr[1])))); $range[1] = long2ip((ip2long($range[0])) + pow(2, (32 - (int)$cidr[1])) - 1); return $range; } $file = 'http://www.example.com/ip_list.txt'; // don't forget http:// $lines = file($file); $ips = []; foreach ($lines as $line) { $ips = array_merge($ips, cidrToRange($line)); } $user_ip = $_SERVER['REMOTE_ADDR']; if (!in_array($user_ip, $ips)) { header('HTTP/1.0 403 Forbidden'); exit; }
/* You need the proper fopen wrapper server settings for reading URLs using file_get_contents, see the notes at the PHP manual */ $allowedIps = explode('\n', file_get_contents('http://my/url/file.txt')); $userIp = $_SERVER['REMOTE_ADDR']; if (!in_array($userIp, $allowedIps)) { exit('Unauthorized'); }