Multi file_get_contents with different results - php

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('https://domain1.com/ips.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:
<?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 '';
}
?>
Without success :)
Thank you for your help!

Consider changing your code to:
$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 '';
}

Related

php check if users ip address is blacklisted and block it from my application

I want write a php function that takes the user's ip address, checks it against known blacklists and redirects users from blacklisted ip addresses to a default "Access Forbidden" page. I only want to allow access to my home page to users from IP addresses that have not been blacklisted. Can anyone help? Here's what I have so far.
<?php
$ip=$_SERVER["REMOTE_ADDR"];
function flush_buffers()
{
ini_set('output_buffering','on');
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush',1);
ob_implicit_flush();
echo ("<html><head><head><body>");
for($i=0;$i<20;$i++) {
echo $i;
echo str_repeat(" ", 500);
ob_flush();
flush();
sleep(1);
}
}
function dnsbllookup($ip)
{
$dnsbl_lookup=array(
"access.redhawk.org",
"b.barracudacentral.org",
"bl.csma.biz",
"bl.emailbasura.org",
"bl.spamcannibal.org",
"bl.spamcop.net",
"bl.technovision.dk",
"blackholes.five-ten-sg.com",
"blackholes.wirehub.net",
"blacklist.sci.kun.nl",
"block.dnsbl.sorbs.net",
"blocked.hilli.dk",
"bogons.cymru.com",
"cart00ney.surriel.com",
"cbl.abuseat.org",
"dev.null.dk",
"dialup.blacklist.jippg.org",
"dialups.mail-abuse.org",
"dialups.visi.com",
"dnsbl.ahbl.org",
"dnsbl.antispam.or.id",
"dnsbl.cyberlogic.net",
"dnsbl.kempt.net",
"dnsbl.njabl.org",
"dnsbl.sorbs.net",
"dnsbl-1.uceprotect.net",
"dnsbl-2.uceprotect.net",
"dnsbl-3.uceprotect.net",
"duinv.aupads.org",
"dul.dnsbl.sorbs.net",
"dul.ru",
"escalations.dnsbl.sorbs.net",
"hil.habeas.com",
"http.dnsbl.sorbs.net",
"intruders.docs.uu.se",
"ips.backscatterer.org",
"korea.services.net",
"mail-abuse.blacklist.jippg.org",
"misc.dnsbl.sorbs.net",
"msgid.bl.gweep.ca",
"new.dnsbl.sorbs.net",
"no-more-funn.moensted.dk",
"old.dnsbl.sorbs.net",
"pbl.spamhaus.org",
"proxy.bl.gweep.ca",
"psbl.surriel.com",
"pss.spambusters.org.ar",
"rbl.schulte.org",
"rbl.snark.net",
"recent.dnsbl.sorbs.net",
"relays.bl.gweep.ca",
"relays.bl.kundenserver.de",
"relays.mail-abuse.org",
"relays.nether.net",
"rsbl.aupads.org",
"sbl.spamhaus.org",
"smtp.dnsbl.sorbs.net",
"socks.dnsbl.sorbs.net",
"spam.dnsbl.sorbs.net",
"spam.olsentech.net",
"spamguard.leadmon.net",
"spamsources.fabel.dk",
"tor.ahbl.org",
"web.dnsbl.sorbs.net",
"whois.rfc-ignorant.org",
"xbl.spamhaus.org",
"zen.spamhaus.org",
"zombie.dnsbl.sorbs.net",
"bl.tiopan.com",
"dnsbl.abuse.ch",
"tor.dnsbl.sectoor.de",
"ubl.unsubscore.com",
"cblless.anti-spam.org.cn",
"dnsbl.tornevall.org",
"dnsbl.anticaptcha.net",
"dnsbl.dronebl.org"
); // Add your preferred list of DNSBL's
$AllCount = count($dnsbl_lookup);
$BadCount = 0;
if($ip)
{
$reverse_ip = implode(".", array_reverse(explode(".", $ip)));
foreach($dnsbl_lookup as $host)
{
if(checkdnsrr($reverse_ip.".".$host.".", "A"))
{
// echo "<span color='#339933'>Listed on ".$reverse_ip.'.'.$host."!</span><br/>";
flush_buffers();
$BadCount++;
}
else
{
// echo "Not listed on ".$reverse_ip.'.'.$host."!<br/>";
flush_buffers();
}
}
}
else
{
// echo "Empty ip!<br/>";
flush_buffers();
}
// echo "This ip has ".$BadCount." bad listings of ".$AllCount."!<br/>";
flush_buffers();
if($BadCount==0)
{
include("index.php");
}
else
{
include("default.htm");
}
}
if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/",#$ip) == true)
{
dnsbllookup($ip);
}?>
the real problem with this is how long it takes.
Made a few changes to show what I mean
average time 60 seconds and that is a long time to wait at a website
<?php
$ip=$_SERVER["REMOTE_ADDR"];
$tstart=time();
echo $ip."<BR>";
function flush_buffers()
{
ini_set('output_buffering','on');
//ini_set('zlib.output_compression', 0);
ini_set('implicit_flush',1);
ob_implicit_flush();
//echo ("<html><head><head><body>");
for($i=0;$i<20;$i++) {
// echo $i;
echo str_repeat(" ", 500);
ob_flush();
flush();
// sleep(1);
}
}
function dnsbllookup($ip)
{
$dnsbl_lookup=array(
"access.redhawk.org",
"b.barracudacentral.org",
"bl.csma.biz",
"bl.emailbasura.org",
"bl.spamcannibal.org",
"bl.spamcop.net",
"bl.technovision.dk",
"blackholes.five-ten-sg.com",
"blackholes.wirehub.net",
"blacklist.sci.kun.nl",
"block.dnsbl.sorbs.net",
"blocked.hilli.dk",
"bogons.cymru.com",
"cart00ney.surriel.com",
"cbl.abuseat.org",
"dev.null.dk",
"dialup.blacklist.jippg.org",
"dialups.mail-abuse.org",
"dialups.visi.com",
"dnsbl.ahbl.org",
"dnsbl.antispam.or.id",
"dnsbl.cyberlogic.net",
"dnsbl.kempt.net",
"dnsbl.njabl.org",
"dnsbl.sorbs.net",
"dnsbl-1.uceprotect.net",
"dnsbl-2.uceprotect.net",
"dnsbl-3.uceprotect.net",
"duinv.aupads.org",
"dul.dnsbl.sorbs.net",
"dul.ru",
"escalations.dnsbl.sorbs.net",
"hil.habeas.com",
"http.dnsbl.sorbs.net",
"intruders.docs.uu.se",
"ips.backscatterer.org",
"korea.services.net",
"mail-abuse.blacklist.jippg.org",
"misc.dnsbl.sorbs.net",
"msgid.bl.gweep.ca",
"new.dnsbl.sorbs.net",
"no-more-funn.moensted.dk",
"old.dnsbl.sorbs.net",
"pbl.spamhaus.org",
"zen.spamhaus.org",
"proxy.bl.gweep.ca",
"psbl.surriel.com",
"pss.spambusters.org.ar",
"rbl.schulte.org",
"rbl.snark.net",
"recent.dnsbl.sorbs.net",
"relays.bl.gweep.ca",
"relays.bl.kundenserver.de",
"relays.mail-abuse.org",
"relays.nether.net",
"rsbl.aupads.org",
"sbl.spamhaus.org",
"smtp.dnsbl.sorbs.net",
"socks.dnsbl.sorbs.net",
"spam.dnsbl.sorbs.net",
"spam.olsentech.net",
"spamguard.leadmon.net",
"spamsources.fabel.dk",
"tor.ahbl.org",
"web.dnsbl.sorbs.net",
"whois.rfc-ignorant.org",
"xbl.spamhaus.org",
"zen.spamhaus.org",
"zombie.dnsbl.sorbs.net",
"bl.tiopan.com",
"dnsbl.abuse.ch",
"tor.dnsbl.sectoor.de",
"ubl.unsubscore.com",
"cblless.anti-spam.org.cn",
"dnsbl.tornevall.org",
"dnsbl.anticaptcha.net",
"dnsbl.dronebl.org"
); // Add your preferred list of DNSBL's
$AllCount = count($dnsbl_lookup);
$BadCount = 0;
if($ip)
{
$reverse_ip = implode(".", array_reverse(explode(".", $ip)));
foreach($dnsbl_lookup as $host)
{
if(checkdnsrr($reverse_ip.".".$host.".", "A"))
{
echo "<span color='#339933'>Listed on ".$reverse_ip.'.'.$host."!</span><br/>";
flush_buffers();
$BadCount++;
}
else
{
// echo "Not listed on ".$reverse_ip.'.'.$host."!<br/>";
flush_buffers();
}
}
}
else
{
// echo "Empty ip!<br/>";
flush_buffers();
}
echo "This ip has ".$BadCount." bad listings of ".$AllCount."!<br/>";
flush_buffers();
if($BadCount==0)
{
// include("index.php");
echo "Not blacklisted ";
}
else
{
// include("default.htm");
echo "Blacklisted";
}
}
if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/",#$ip) == true)
{
dnsbllookup($ip);
}
$tend=time();
$tvar=$tend-$tstart;
echo "<BR> took $tvar seconds <br>";
?>
After pondering the nice but slow solution above, I came up with a much-simplified listing of bad IPs that returns only TRUE (if blacklisted) or FALSE (if it is not). Not as all-inclusive as the above, of course, but it seems to cover any test I could throw at it and it is quite fast.
Unremark the $UserIP value at the top to see one that fails or pass your own to (hopefully) see one that does not. Although I didn't actually time it, it seems to load quickly, especially as all of the blocklist.de sites have only raw IPs and nothing else to have to filter through. In fact, perhaps SpamHouse alone would do the job for most users.
Credit for this code is James who posted the another example and I just simplified it and I also eliminated flush_buffers() as I don't see a need for it.
function dnsblLookup($UserIP) {
//$UserIP = "216.145.14.142";
$dnsbl_lookup=array(
"blocklist.de/lists/ssh.txt",
"blocklist.de/lists/apache.txt",
"blocklist.de/lists/asterisk.txt",
"blocklist.de/lists/bots.txt",
"blocklist.de/lists/courierimap.txt",
"blocklist.de/lists/courierpop3.txt",
"blocklist.de/lists/email.txt",
"blocklist.de/lmostists/ftp.txt",
"blocklist.de/lists/imap.txt",
"blocklist.de/lists/pop3.txt",
"blocklist.de/lists/postfix.txt",
"blocklist.de/lists/proftpd.txt",
"blocklist.de/lists/sip.txt",
"ciarmy.com/list/ci-badguys.txt",
"sbl.spamhaus.org",
"xbl.spamhaus.org",
"zen.spamhaus.org"
);
$BadCount = 0;
if ($UserIP) :
$reverse_ip = implode(".", array_reverse(explode(".", $UserIP)));
foreach($dnsbl_lookup as $host) :
if (checkdnsrr($reverse_ip.".".$host.".", "A")) :
$BadCount++;
if ($BadCount > 0) :
break;
endif;
endif;
endforeach;
endif;
if ($BadCount == 0) :
return FALSE;
else :
return TRUE;
endif;
}

php script does not work. but if I include it in other file, it works?

I have this half-written script, and it will not work at all
<?php
function hit_count() {
echo $ip_address = $_SERVER['REMOTE_ADDR'];
$ip_file = file('ip.txt');
foreach ($ip_file as $ip) {
$ip_single = trim($ip);
if ($ip_address==$ip_single) {
$found = false;
} else {
$found = true;
}
}
if ($found==false) {
echo 'IP not found.';
}
}
?>
even the first line will not display anything.
but lo and behold, if I include it in this file
<?php
include 'unique-counter.php';
hit_count();
?>
the "echo" displays my ip.
how does that happen?
Your script by itself only defines a function, it does not call it.
Add hit_count(); to the standalone script.

why can't array_search find the value if the code of the key is "gb2312"?

my questions:
$state=array("你"=>1);
if(array_key_exists("你",$state))
{
$result = array_search("你",$state);echo $result;
}else
{
echo "No Exists";
}
i expect the result of "1", however the output is "No Exists", i don't know why the program can't get the value of the key "你".
array_search will search the given array by value. Try the following:
$state = array("你"=>1);
if(array_key_exists("你", $state)) {
echo $state["你"];
} else {
echo "No Exists";
}
// => 1
» demo
Hope below function will help.
<?php
$array = array('arr1'=>array('find_me'=>'yes you did.'));
function get_value_by_key($array,$key)
{
foreach($array as $k=>$each)
{
if($k==$key)
{
return $each;
}
if(is_array($each))
{
if($return = get_value_by_key($each,$key))
{
return $return;
}
}
}
}
echo get_value_by_key($array,'find_me');
?>
the encoding type of the show paper and the store paper is GB2312.
$state=array("你"=>1);
if(array_key_exists("你",$state))
{
$result1 = $state["你"];
echo $result1; // can get the value 111
}else
{
echo "No Exists";
}
the code above can be executed rightly. i can't show my problems accurately. Now i paste out my code , there is some questions.
<?php
$file = file("GB2312-HanZiBianMa.txt"); // file encoding type ANSI
foreach ($file as $line_num => $line)
{
list($no,$hex,$dec) = preg_split('[\t]',htmlspecialchars($line));;
$result[$hex] = $dec;
}
$result_2 = array_flip($result);
if(array_key_exists("你",$result_2)) // **can't find the value** 222
{
$state= $result_2["你"];
echo $state."<br/>";
}else
{
echo "No Exists<br/>";
}
foreach($result_2 as $k=>$each) //can get the value using the preg_match
{
if(preg_match('/你/', $k))
echo $k."\t".$each."<br/>";
}
?>
the format of GB2312-HanZiBianMa.txt is as follows:
1947 c4e3 你
1948 c4e4 匿
1949 c4e5 腻
1950 c4e6 逆
if your want to test the code , you can save the php code and save the GB2312.. file.
the question is:
why can't the following function get the right value ? the data comes from file and one stores together.
if(array_key_exists("你",$result_2)) // **can't find the value** 222
{
$state= $result_2["你"];
echo $state."<br/>";
}else
{
echo "No Exists<br/>";
}

in_array not working explode

I am not able to figure out why my condition is not working while the ip address is in the array. Why condition is failing as shown in image
<?php $valid_ip_list = explode(',',$this->valid_ips);
echo $client_ip = $_SERVER['REMOTE_ADDR'];
print('<pre>');
print_r($valid_ip_list);
if(in_array($client_ip ,$valid_ip_list))
{
echo 'I am here';
}
else
{
echo 'Condition fail';
}
?>
Problem solved with the help of array_map('trim', explode(',', $valid_ips))
This should help
$valid_ips = '192.100.100.61,192.100.100.2,127.0.0.1';
// authorized
if (in_array($_SERVER['REMOTE_ADDR'], array_map("trim", explode(',', $valid_ips)))) {
//...
}
// unauthorized
else {
//...
}

read file with ip range and see if supplied IP matches any range in file

I am trying to read a file with ip/mask ranges and if the supplied IP matches any range in the file it will return with TRUE or similar function. Here is the code I have below
function myip2long($ip) {
if (is_numeric($ip)) {
return sprintf("%u", floatval($ip));
} else {
return sprintf("%u", floatval(ip2long($ip)));
}
}
function ipfilter($ip) {
$match = 0;
$ip_addr = decbin(myip2long($ip));
if (file_get_contents('./countryip/all-zones/us.zone')) {
$source = file('./countryip/all-zones/us.zone');
foreach ($source as $line) {
$network = explode("/", $line);
$net_addr = decbin(myip2long($network[0]));
$cidr = $network[1];
if (substr($net_addr, 0, $cidr) == substr($ip_addr, 0, $cidr)) {
$match = 1;
break;
}
}
}
return $match;
}
$user_ip = $_SERVER['REMOTE_ADDR'];
if (ipfilter($user_ip) == 1) echo "<br />allowed! Your IP is a United States IP!";
else echo "deny!";
An example file (like the one in the example above) is available here
http://www.ipdeny.com/ipblocks/data/countries/us.zone
Not sure if the code above is correct, I got it from here'
http://www.php.net/manual/en/function.ip2long.php#86793
Probably you should add some debug code, just to understand what is going on.
Just like this:
if (substr($net_addr, 0, $cidr) == substr($ip_addr, 0, $cidr)) {
echo "My IP: $ip\n";
echo "IP to check: $network[0]\n";
echo "CIDR: $cidr\n"
echo "ip digits, my: $ip_addr, check: $net_addr\n";
$match = 1;
break;
}
So you'll see what is going wrong.

Categories