New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
PHP Experts needed: add 3-4 file_get_contents and to show different results
Id like to add 3-4 file_get_contents and to show different results. This is what am using now:
<?php $listOfIPS = explode("\n", file_get_contents('http://domain1.com/file1.txt')); if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) { echo '{"type":"Valid"}'; } else { echo ''; } ?>
Now I want to search for IP's in 3 .txt files at once.
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"}
I have tried like this but dident worked:
<?php $listOfIPS = explode("\n", file_get_contents('http://domain1.com/file1.txt')); $listOfIPS2 = explode("\n", file_get_contents('http://domain1.com/file2.txt')); $listOfIPS3 = explode("\n", file_get_contents('http://domain1.com/file3.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 ''; } ?>
Thank you very much for your help.
Comments
@traffic where are you
Does it always return ""? Have you tried using \r\n instead of \n?
ALways should return "" if the ip is not on those files.
I mean always should return to this if the ip is not found in one of those 3 txt files:
What do you mean "it didn't work", can you show some cases where it didn't work? For example, in file_a.txt, I have this. I run my code, the output should be "x" but I got "y" instead.
The IP is inside the file1.txt and it not shows {"type":"Valid"}
It shows the else result:
If I use only the first example only with one file1.txt it works like a charm.
try to var_dump($listOfIPS).. probably there is a space at the end of each line with IP
nvm then
here you go
if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) {
echo '{"type":"Valid"}';
}
elseif (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS2)) {
echo '{"type":"invalid"}';
}
elseif (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS3)) {
echo '{"type":"fix"}';
}
else {
echo '';
}
where is the domain1.com/file1.txt?
just replace the corresponding block. you need to use elseif's instead of if's after the first if.
dident worked
var_dump($listOfIPS); var_dump($listOfIPS2); var_dump($listOfIPS3); please
Unless you provide us with actual testing data, as requested before, no one will be able to help you and you'll just be wasting our time.
still not
I provided everything.
What do you mean? var_dump() will dump the contents of the variables so we can see what's in the variable. It's useful debugging information.
Was showing blank page also this:
maybe I should add something like this: $listOfIPS = explode("\n", file_get_contents('http://domain1.com/file1.txt','http://domain1.com/file2.txt','http://domain1.com/file3.txt'));
So am using the code from this thread: https://www.lowendtalk.com/discussion/73585/php-script-help#latest
Now I want to add more files as I explained.
Don't use file_get_contents to access remote files. Potentially lot of problems.
And it is slow anyway.
You must add the var_dump() to the end of the file. It just dumps the contents of said variables.
Where exacly should I add it?
At the end of the file, after the last echo '';
Same result. Not working.
So if the ip exitst in file1.txt should show the output of it. But if the ip is in non of files should show echo '';
Seems that the script like it is now is asking to be the IP in all 3 files or something.
Can you tell what is the usecase... An IP could be in one / all / none of the text files and if you are running them in a else if loop will produce inaccurate results.
If my ip (pc IP) is added in file1.txt the output should be {"type":"Valid"}
If my ip (pc IP) is not added in file1.txt the output should be echo '';
Now this works if I use it only with one file. If I add more file to be checked is not working.
Still not
I have tested it on localhost,and it's working .
You may also use this line for testing ..
echo $_SERVER['REMOTE_ADDR'];
Here you go: http://pastebin.com/kVheFtBr
With this implementation, your script won't blow even if the text files are yuuuuge.
Make sure to replace the filenames with yours and the
$targetIP
with$_SERVER['REMOTE_ADDR']