CaptainJoe54 Posted August 30, 2017 Share Posted August 30, 2017 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 Share on other sites More sharing options...
requinix Posted August 30, 2017 Share Posted August 30, 2017 And the problem is... what? Link to comment Share on other sites More sharing options...
CaptainJoe54 Posted August 30, 2017 Author Share Posted August 30, 2017 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 Share on other sites More sharing options...
requinix Posted August 30, 2017 Share Posted August 30, 2017 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 Share on other sites More sharing options...
CaptainJoe54 Posted August 30, 2017 Author Share Posted August 30, 2017 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 Share on other sites More sharing options...
requinix Posted August 30, 2017 Share Posted August 30, 2017 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 Share on other sites More sharing options...
CaptainJoe54 Posted August 30, 2017 Author Share Posted August 30, 2017 hmm looking at PHP doucumentation you mean something like this or literal dereference?. $reqstring = $jresult[""]["id"]; Link to comment Share on other sites More sharing options...
requinix Posted August 30, 2017 Share Posted August 30, 2017 Except instead of [""] you use [0], yes. If there's no string key then the key is a number counting from 0. So $jresult[0]["id"] Link to comment Share on other sites More sharing options...
CaptainJoe54 Posted August 30, 2017 Author Share Posted August 30, 2017 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 Share on other sites More sharing options...
requinix Posted August 30, 2017 Share Posted August 30, 2017 Yes: if ($test = "Too Many Requests") { Link to comment Share on other sites More sharing options...
CaptainJoe54 Posted August 30, 2017 Author Share Posted August 30, 2017 if ($test == "Too Many Requests") { Link to comment Share on other sites More sharing options...
requinix Posted August 30, 2017 Share Posted August 30, 2017 Link to comment Share on other sites More sharing options...
CaptainJoe54 Posted August 30, 2017 Author Share Posted August 30, 2017 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 Share on other sites More sharing options...
requinix Posted August 31, 2017 Share Posted August 31, 2017 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 Share on other sites More sharing options...
CaptainJoe54 Posted August 31, 2017 Author Share Posted August 31, 2017 Yeah your right. JSON dump// 1 reported ipip "156.67.143.204"country "Germany"isoCode "DE"category 0 10created "Thu, 31 Aug 2017 13:58:57 +0000"//5 reported ips0 ip "112.65.179.46"country "China"isoCode "CN"category 0 22created "Thu, 31 Aug 2017 14:00:51 +0000"1 ip "112.65.179.46"country "China"isoCode "CN"category 0 22created "Wed, 30 Aug 2017 13:18:07 +0000"2 ip "112.65.179.46"country "China"isoCode "CN"category 0 51 15created "Mon, 28 Aug 2017 04:24:05 +0000"3 ip "112.65.179.46"country "China"isoCode "CN"category 0 51 15created "Sun, 27 Aug 2017 04:24:59 +0000"4 ip "112.65.179.46"country "China"isoCode "CN"category 0 51 15created "Sat, 26 Aug 2017 11:17:52 +0000"// no bad ip foundempty 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.." Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.