多个file_get_contents具有不同的结果

时间:2016-02-26 10:48:13

标签: php

我想添加3-4个file_get_contents并显示不同的结果。这就是我现在正在使用的:

<?php
$listOfIPS = explode("\n", file_get_contents('https://domain1.com/ips.txt'));

if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) {
    echo '{"type":"Valid"}';
}
else {
    echo '';
}
?>

现在我想一次在3个.txt文件中搜索IP。

If ip found on file1.txt should show content {"type":"Valid"}
If ip found on file2.txt should show content {"type":"invalid"}
If ip found on file3.txt should show content {"type":"fix"}

我试过这样的话:

<?php
$listOfIPS = explode("\n", file_get_contents('https://domain1.com/ips.txt'));
$listOfIPS2 = explode("\n", file_get_contents('https://domain1.com/ips2.txt'));
$listOfIPS3 = explode("\n", file_get_contents('https://domain1.com/ips3.txt'));

if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) {
    echo '{"type":"Valid"}';
}
if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS2)) {
    echo '{"type":"invalid"}';
}
if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS3)) {
    echo '{"type":"fix"}';
}

else {
    echo '';
}
?>

没有成功:)

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

考虑将您的代码更改为:

$listOfIPS = explode(PHP_EOL, file_get_contents('https://domain1.com/ips.txt'));
$listOfIPS2 = explode(PHP_EOL, file_get_contents('https://domain1.com/ips2.txt'));
$listOfIPS3 = explode(PHP_EOL, file_get_contents('https://domain1.com/ips3.txt'));

if (!empty($listOfIPS) && in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) {
    echo '{"type":"Valid"}';
}
if (!empty($listOfIPS2) && in_array($_SERVER['REMOTE_ADDR'], $listOfIPS2)) {
    echo '{"type":"invalid"}';
}
if (!empty($listOfIPS3) && in_array($_SERVER['REMOTE_ADDR'], $listOfIPS3)) {
    echo '{"type":"fix"}';
}
else {
    echo '';
}