Skip to content

Commit cc7c499

Browse files
committed
Updated client IP address detection logic
1 parent f2e0f47 commit cc7c499

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ crashlytics.properties
4949
crashlytics-build.properties
5050

5151
### Composer template
52-
# composer.phar
52+
composer.phar
5353
composer.lock
5454
vendor/
5555

src/Controllers/AbstractOAuth2Controller.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct()
5151
];
5252
}
5353
if (!is_null($clientIpAddress = $this->getClientIP())) {
54-
$this->headers['X-Forwarded-For'] = $clientIpAddress;
54+
$this->headers['X-Forwarded-For'] = $clientIpAddress;
5555
}
5656
$this->encryption = DI::container()->get(EncryptionProvider::class);
5757
$this->secureStorage = DI::container()->get(SecureCookieProvider::class);
@@ -111,22 +111,25 @@ public function setEncryptionProvider(EncryptionInterface $encryptionProvider):
111111
}
112112

113113
/**
114-
* looks for a user's IP address
114+
* looks for a user's IP address
115115
*
116116
* @return string|null
117117
*/
118118
public function getClientIP()
119-
{
120-
if (array_key_exists('HTTP_X_REAL_IP', $_SERVER)) {
121-
return $_SERVER["HTTP_X_REAL_IP"];
122-
} else if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
123-
return $_SERVER["HTTP_X_FORWARDED_FOR"];
124-
} else if (array_key_exists('REMOTE_ADDR', $_SERVER)) {
125-
return $_SERVER["REMOTE_ADDR"];
126-
} else if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
127-
return $_SERVER["HTTP_CLIENT_IP"];
119+
{
120+
$potentialHeaders = [
121+
'HTTP_X_REAL_IP', // nginx
122+
'HTTP_X_FORWARDED_FOR',
123+
'REMOTE_ADDR',
124+
'HTTP_CLIENT_IP'
125+
];
126+
127+
foreach ($potentialHeaders as $header) {
128+
if (!empty($value = $_SERVER[$header] ?? null)) {
129+
return $value;
130+
}
128131
}
129-
132+
130133
return null;
131134
}
132135

0 commit comments

Comments
 (0)