Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


PHP Experts needed: add 3-4 file_get_contents and to show different results
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.

PHP Experts needed: add 3-4 file_get_contents and to show different results

WHTWHT Member

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.

«1

Comments

  • @traffic where are you :)

  • Does it always return ""? Have you tried using \r\n instead of \n?

    Thanked by 1WHT
  • WHTWHT Member
    edited February 2016

    @Traffic said:
    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:

    echo '';

  • 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.

    Thanked by 1WHT
  • WHTWHT Member
    edited February 2016

    @black said:
    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:

    echo '';

  • If I use only the first example only with one file1.txt it works like a charm.

  • exception0x876exception0x876 Member, Host Rep, LIR
    edited February 2016

    try to var_dump($listOfIPS).. probably there is a space at the end of each line with IP

    @WHT said:
    If I use only the first example only with one file1.txt it works like a charm.

    nvm then

  • exception0x876exception0x876 Member, Host Rep, LIR
    edited February 2016

    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 '';
    }

    Thanked by 1MacPac
  • @exception0x876 said:
    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?

  • exception0x876exception0x876 Member, Host Rep, LIR

    just replace the corresponding block. you need to use elseif's instead of if's after the first if.

  • @exception0x876 said:
    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.

    Thanked by 1netomx
  • @black said:
    var_dump($listOfIPS); var_dump($listOfIPS2); var_dump($listOfIPS3); please

    still not :(

  • @luissousa said:
    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.

    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.

  • @black said:
    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:

    echo '';

  • 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.

    Thanked by 1netomx
  • @Traffic said:
    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?

  • TrafficTraffic Member
    edited February 2016

    @WHT said:

    At the end of the file, after the last echo '';

    Thanked by 1netomx
  • @Traffic said:

    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.

  • @upfreak said:
    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.

  • <?php
    $listOfIPS = explode("\n", file_get_contents('http://domain.com/file1.txt'));
    $listOfIPS2 = explode("\n", file_get_contents('http://domain.com/file2.txt'));
    $listOfIPS3 = explode("\n", file_get_contents('http://domain.com/file3.txt'));
    
    if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) {
    echo '{"type":"Valid"}';
    }else if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS2)) {
    echo '{"type":"invalid"}';
    }else if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS3)) {
    echo '{"type":"fix"}';
    }else {
    echo '';
    }
    ?>
    
  • @Shamildx said:
    <?php > $listOfIPS = explode("\n", file_get_contents('http://domain.com/file1.txt')); > $listOfIPS2 = explode("\n", file_get_contents('http://domain.com/file2.txt')); > $listOfIPS3 = explode("\n", file_get_contents('http://domain.com/file3.txt')); > > if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) { > echo '{"type":"Valid"}'; > }else if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS2)) { > echo '{"type":"invalid"}'; > }else if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS3)) { > echo '{"type":"fix"}'; > }else { > echo ''; > } > ?>

    Still not

  • WHT said: Still not

    I have tested it on localhost,and it's working .

    You may also use this line for testing ..
    echo $_SERVER['REMOTE_ADDR'];

  • deadbeefdeadbeef Member
    edited February 2016

    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']

Sign In or Register to comment.