• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

247
Views
¿Cómo cargar una lista de rangos de ip para usarla con el script $allowedIps?

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
almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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; }
almost 3 years ago · Juan Pablo Isaza Report

0

/* 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'); }

PHP manual para file_get_contents

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error