/** * Fetch the IP Address. * * @return string */ public function ip_address() { if ($this->ip_address !== NULL) { return $this->ip_address; } if ($ip = $this->server('HTTP_CLIENT_IP')) { $this->ip_address = $ip; } elseif ($ip = $this->server('REMOTE_ADDR')) { $this->ip_address = $ip; } elseif ($ip = $this->server('HTTP_X_FORWARDED_FOR')) { $this->ip_address = $ip; } if ($comma = strrpos($this->ip_address, ',') !== FALSE) { $this->ip_address = substr($this->ip_address, $comma + 1); } if (!valid::ip($this->ip_address)) { // Use an empty IP $this->ip_address = '0.0.0.0'; } return $this->ip_address; }
public function valid_ip_test() { $this->assert_true_strict(valid::ip('72.14.204.147'))->assert_true_strict(valid::ip('127.0.0.1'))->assert_false_strict(valid::ip('256.257.258.259'))->assert_false_strict(valid::ip('255.255.255.255'))->assert_false_strict(valid::ip('192.168.0.1')); // invalid - private range }
/** * Fetch the IP Address. * * @return string */ public function ip_address() { if ($this->ip_address !== NULL) { return $this->ip_address; } // Server keys that could contain the client IP address $keys = array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR'); foreach ($keys as $key) { if ($ip = $this->server($key)) { $this->ip_address = $ip; // An IP address has been found break; } } if ($comma = strrpos($this->ip_address, ',') !== FALSE) { $this->ip_address = substr($this->ip_address, $comma + 1); } if (!valid::ip($this->ip_address)) { // Use an empty IP $this->ip_address = '0.0.0.0'; } return $this->ip_address; }
/** * Tests the valid::ip() function. * @dataProvider ip_provider * @group core.helpers.valid.ip * @test */ public function ip($input_ip, $allow_private, $expected_result) { $result = valid::ip($input_ip, FALSE, $allow_private); $this->assertEquals($expected_result, $result); }