mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 17:12:17 +00:00
Merge pull request #504 from DeveloperMarius/get-ip-proxy
Get the ip when the user is using a proxy
This commit is contained in:
@@ -213,15 +213,29 @@ class Request
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get id address
|
* Get id address
|
||||||
|
* If $safe is false, this function will detect Proxys. But the user can edit this header to whatever he wants!
|
||||||
|
* https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php#comment-25086804
|
||||||
|
* @param bool $safe
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getIp(): ?string
|
public function getIp(bool $safe = false): ?string
|
||||||
{
|
{
|
||||||
return $this->getFirstHeader([
|
$client_header = null;
|
||||||
'http-cf-connecting-ip',
|
if(!$safe){
|
||||||
'http-x-forwarded-for',
|
$client_header = $this->getHeader(
|
||||||
'remote-addr',
|
'http-cf-connecting-ip',
|
||||||
]);
|
$this->getHeader(
|
||||||
|
'http-client-ip',
|
||||||
|
$this->getHeader(
|
||||||
|
'http-x-forwarded-for',
|
||||||
|
$this->getHeader('remote-addr')
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if($client_header === null)
|
||||||
|
$client_header = $this->getHeader('remote-addr');
|
||||||
|
return filter_var($client_header, FILTER_VALIDATE_IP) ? $client_header : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user