/** * Validate the syntax of the given IPv4 adress. * * @param string IPv4 adress * @return boolean true if syntax is valid, otherwise false * @see IPv4::check() **/ public static function checkIP($ip) { return IPv4::check($ip); }
private function parseIP() { $ips = array(); $indices = array('REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP'); foreach ($indices as $index) { $tip = @getenv($index); if (!empty($tip)) { $ips[] = $tip; } if (!empty($_SERVER[$index])) { $ips[] = $_SERVER[$index]; } } $ips = array_unique($ips); foreach ($ips as $key => $ip) { if (IPv4::check($ip)) { if (IPv4::checkPrivate($ip)) { return $ip; } } else { unset($ips[$key]); } } // Try a private one if (count($ips) > 0) { return end($ips); } else { // No ip found return null; } }
function cb_captcha_check($recaptcha_response_field) { $ip = new IPv4(); $resp = recaptcha_check_answer(Config::get('captcha.private_key'), $ip->getIP(), Request::get('recaptcha_challenge_field'), $recaptcha_response_field); return $resp->is_valid; }
/** * @dataProvider privateAddresses */ public function testIsPrivate($ip) { $ip = new IPv4($ip); $this->assertTrue($ip->isPrivate(), "{$ip} is private"); $this->assertFalse($ip->isPublic(), "{$ip} is not public"); }