Skip to content

Commit

Permalink
Merge pull request #504 from DeveloperMarius/get-ip-proxy
Browse files Browse the repository at this point in the history
Get the ip when the user is using a proxy
  • Loading branch information
skipperbent authored Mar 25, 2021
2 parents 8835aca + 4cb2fa5 commit 11313a3
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/Pecee/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,29 @@ public function getHeaders(): array

/**
* 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
*/
public function getIp(): ?string
{
return $this->getFirstHeader([
'http-cf-connecting-ip',
'http-x-forwarded-for',
'remote-addr',
]);
public function getIp(bool $safe = false): ?string
{
$client_header = null;
if(!$safe){
$client_header = $this->getHeader(
'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;
}

/**
Expand Down

0 comments on commit 11313a3

Please sign in to comment.