Input data:
ip => 123.1.1.3 There is a list of .txt:
123.123.33.3 127.0.0.1 ... You need a function that returns true if the list contains the IP of their input data or returns false if the IP is not in the list.
Input data:
ip => 123.1.1.3 There is a list of .txt:
123.123.33.3 127.0.0.1 ... You need a function that returns true if the list contains the IP of their input data or returns false if the IP is not in the list.
The first way.
If the list is small, you can use the file function to read the file and put it into an array, and then use array_search to search the array for a value. array_search searches for the given value in the array and returns the corresponding key in case of success.
IPs.txt file
123.123.33.3 127.0.0.1 Search
$ipToSearch = '123.1.1.3'; $ipList = file('IPs.txt'); echo array_search($ipToSearch, $ipList) ? 'YO!' : 'NO :-('; The second way: use the loop (and the function as a wrapper, so as not to stick out the code :-))
function findIP($ipToSearch, $file) { $fp = fopen($file, 'r'); while (!feof($fp)) { $line = trim(fgets($fp)); if ($ipToSearch == $line) return true; } fclose($fp); return false; } echo findIP('123.1.1.3', 'IPs.txt') ? 'YO!' : 'NO :-('; fopen - Opens file or URL
fclose - closes accordingly
fgets - Reads a line from a file
ps Also you should not forget error handling for the presence of a file, for an unsuccessful attempt to read, during reading, etc.
<?php $ip = "123.1.1.3"; $file = file_get_contents("ip.txt"); $array = explode(" ", $file); if (in_array($ip, $array)) return true; // есть в файле else return false; // нету Source: https://ru.stackoverflow.com/questions/507924/
All Articles
filephp.net/manual/ru/function.file.php to read the file and translate it into an array, and then usearray_searchphp.net/manual/ru/function.array-search.php to search in the array of values - Alexey Shimansky