Jump to content

Recommended Posts

Hi im trying to add another check so that if rate is exceeded redirect does not execute.
And yeah im a newbie when it comes to PHP.

 

Basically pseudocode:
if (abuseipdb_check($key,$_SERVER['REMOTE_ADDR'],30) > 0 or id: != "Too Many Requests" ) { header("Location: $redirect"); }

 

JSON    
0:    
id:    "Too Many Requests"
links:    
about:    "https://www.abuseipdb.com/api"

<?php
/*
	ABUSEIPDB IP CHECK
	Ver	: 1.1
	Author	: Barret
	Date	: 2017/5
	
	This script is not part nor made by abuseipdb.com
*/
// Your AbuseIPdb.com API key (register at website to get one)
$key		= "<your api key>";
// Url to redirect user if any reports are present
$redirect 	= "http://www.google.com";
// Fetch report count with given parameters and take action if condition is met
if (abuseipdb_check($key,$_SERVER['REMOTE_ADDR'],30) > 0) { header("Location: $redirect"); }
function abuseipdb_check($key,$target,$days) {
	return count(json_decode(abuseipdb_geturl("https://www.abuseipdb.com/check/$target/json?key=$key&days=$days"),true));
}
function abuseipdb_geturl($url, $useragent='PHP/CURL', $headers=false, $follow_redirects=true, $debug=false) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	if ($headers==true){ curl_setopt($ch, CURLOPT_HEADER,1); }
    if ($headers=='headers only') { curl_setopt($ch, CURLOPT_NOBODY ,1); }
    if ($follow_redirects==true) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); }
    if ($debug==true) {
        $result['contents']=curl_exec($ch);
        $result['info']=curl_getinfo($ch);
    }
    else $result=curl_exec($ch);
    curl_close($ch);
    return $result;
}
?>
Link to comment
https://forums.phpfreaks.com/topic/304798-json-compare-string/
Share on other sites

Not sure how to access the structure I guess and return proper value.

<?php

	$key = "<your api key>";
	// Url to redirect user if any reports are present
	$redirect 	= "https://www.google.com";
	// Fetch report count with given parameters and take action if condition is met
	if (abuseipdb_check($key,$_SERVER['REMOTE_ADDR'],30) > 0) { header("Location: $redirect"); }

function abuseipdb_check($key,$target,$days) {	
	$ipresult = abuseipdb_geturl("https://www.abuseipdb.com/check/$target/json?key=$key&days=$days");
	$jresult = json_decode($ipresult,true);
	if (in_array("Too Many Requests", $jresult)) {
		return 0;
	}
	return count($jresult);
}

function abuseipdb_geturl($url, $useragent='PHP/CURL', $headers=false, $follow_redirects=true, $debug=false) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	if ($headers==true){ curl_setopt($ch, CURLOPT_HEADER,1); }
	if ($headers=='headers only') { curl_setopt($ch, CURLOPT_NOBODY ,1); }
	if ($follow_redirects==true) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); }
	if ($debug==true) {
		$result['contents']=curl_exec($ch);
		$result['info']=curl_getinfo($ch);
	}
	else $result=curl_exec($ch);
	curl_close($ch);
	return $result;
}
Link to comment
https://forums.phpfreaks.com/topic/304798-json-compare-string/#findComment-1550399
Share on other sites

Assuming the response was

[
	{
		"id": "Too Many Requests",
		"links": ???,
		"about": "https://www.abuseipdb.com/api"
	}
]
then it would decode to

array(
	array(
		"id" => "Too Many Requests",
		"links" => ???,
		"about" => "https://www.abuseipdb.com/api"
	)
)
Do you see how to get it now?
Link to comment
https://forums.phpfreaks.com/topic/304798-json-compare-string/#findComment-1550400
Share on other sites

This works found function on stackoverflow.com but if there's better way to do it with less code etc.. show me.

<?php

	$key = "<your api key>";
	// Url to redirect user if any reports are present
	$redirect 	= "https://www.google.com";
	// Fetch report count with given parameters and take action if condition is met
	if (abuseipdb_check($key,$_SERVER['REMOTE_ADDR'],30) > 0) { header("Location: $redirect"); }

function abuseipdb_check($key,$target,$days) {	
	$ipresult = abuseipdb_geturl("https://www.abuseipdb.com/check/$target/json?key=$key&days=$days");
	$jresult = json_decode($ipresult,true);
	$test = in_array_r("Too Many Requests", $jresult);
	if ($test = true) {
	   return 0;
	}
	return count($jresult);
}

function abuseipdb_geturl($url, $useragent='PHP/CURL', $headers=false, $follow_redirects=true, $debug=false) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	if ($headers==true){ curl_setopt($ch, CURLOPT_HEADER,1); }
	if ($headers=='headers only') { curl_setopt($ch, CURLOPT_NOBODY ,1); }
	if ($follow_redirects==true) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); }
	if ($debug==true) {
		$result['contents']=curl_exec($ch);
		$result['info']=curl_getinfo($ch);
	}
	else $result=curl_exec($ch);
	curl_close($ch);
	return $result;
}

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}
Link to comment
https://forums.phpfreaks.com/topic/304798-json-compare-string/#findComment-1550403
Share on other sites

So are you saying that given a variable

$jresult = array(
	array(
		"id" => "Too Many Requests",
		"links" => ???,
		"about" => "https://www.abuseipdb.com/api"
	)
);
you do not know how to get to the "Too Many Requests" string? I'm trying to prod your brain in the right direction with the assumption that you do, in fact, know how and just haven't realized you can do it.

 

Because yes, using that in_array_r is ridiculous overkill. Plus your code is buggy.

Link to comment
https://forums.phpfreaks.com/topic/304798-json-compare-string/#findComment-1550406
Share on other sites

Okey thx, one more thing though is the code still buggy?.

<?php
	$key = "<your api key>";
	// Url to redirect user if any reports are present
	$redirect 	= "https://www.google.com";
	// Fetch report count with given parameters and take action if condition is met
	if (abuseipdb_check($key,$_SERVER['REMOTE_ADDR'],30) > 0) { header("Location: $redirect"); }

function abuseipdb_check($key,$target,$days) {	
	$ipresult = abuseipdb_geturl("https://www.abuseipdb.com/check/$target/json?key=$key&days=$days");
	$jresult = json_decode($ipresult,true);
	$test = $jresult[0]["id"];
	if ($test = "Too Many Requests") {
	   return 0;
	}
	return count($jresult);
}

function abuseipdb_geturl($url, $useragent='PHP/CURL', $headers=false, $follow_redirects=true, $debug=false) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	if ($headers==true){ curl_setopt($ch, CURLOPT_HEADER,1); }
	if ($headers=='headers only') { curl_setopt($ch, CURLOPT_NOBODY ,1); }
	if ($follow_redirects==true) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); }
	if ($debug==true) {
		$result['contents']=curl_exec($ch);
		$result['info']=curl_getinfo($ch);
	}
	else $result=curl_exec($ch);
	curl_close($ch);
	return $result;
}
Link to comment
https://forums.phpfreaks.com/topic/304798-json-compare-string/#findComment-1550424
Share on other sites

Is this okey?.

	$ipresult = abuseipdb_geturl("https://www.abuseipdb.com/check/$target/json?key=$key&days=$days");
	$jresult = json_decode($ipresult,true);
	// No IP recordes found
	if ($jresult == NULL) {
	   return 0;
	}
	$jscount = count($jresult);
	// ip check limit exceeded
	$iplimit = isset($jresult[0]['id']);
	if ($iplimit == true) {
	   return 0;
	}
	// ip record(s) found
	$ipabuses = isset($jresult[0]['ip']);
	if ($ipabuses == true) {
	   return $jscount;
	}
	// ip record found
	$ipabuse = isset($jresult['ip']);
	if ($ipabuse == true) {
	   return $jscount;
	}
Link to comment
https://forums.phpfreaks.com/topic/304798-json-compare-string/#findComment-1550466
Share on other sites

I don't know what the JSON is for successful API calls, but I would think that simply testing for the existence of a value - which is what isset does - is not correct. Surely you should be looking at what the values actually are?

Link to comment
https://forums.phpfreaks.com/topic/304798-json-compare-string/#findComment-1550476
Share on other sites

Yeah your right.

 

JSON dump

// 1 reported ip
ip    "156.67.143.204"
country    "Germany"
isoCode    "DE"
category    
0    10
created    "Thu, 31 Aug 2017 13:58:57 +0000"



//5 reported ips
0    
ip    "112.65.179.46"
country    "China"
isoCode    "CN"
category    
0    22
created    "Thu, 31 Aug 2017 14:00:51 +0000"
1    
ip    "112.65.179.46"
country    "China"
isoCode    "CN"
category    
0    22
created    "Wed, 30 Aug 2017 13:18:07 +0000"
2    
ip    "112.65.179.46"
country    "China"
isoCode    "CN"
category    
0    5
1    15
created    "Mon, 28 Aug 2017 04:24:05 +0000"
3    
ip    "112.65.179.46"
country    "China"
isoCode    "CN"
category    
0    5
1    15
created    "Sun, 27 Aug 2017 04:24:59 +0000"
4    
ip    "112.65.179.46"
country    "China"
isoCode    "CN"
category    
0    5
1    15
created    "Sat, 26 Aug 2017 11:17:52 +0000"

// no bad ip found
empty json Raw Data []


// error message very similar to ip rate limit with id, links, about etc...
0    
id    "Unprocessable Entity"
links    
about    "https://www.abuseipdb.com/api"
status    "422"
code    "1002"
title    "The request was well-formed but was unable to be followed due to semantic errors."
detail    "We expected an IPv4 or IPv6 address (e.g. 8.8.8.8)."

Link to comment
https://forums.phpfreaks.com/topic/304798-json-compare-string/#findComment-1550496
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.