Skip to content

Commit 2b577e6

Browse files
committed
Update Request::getIp method
1 parent 23d8694 commit 2b577e6

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/Request.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,13 @@ public function getId() : ?string
895895
*/
896896
public function getIp() : string
897897
{
898-
return $_SERVER[$this->getIpKey()];
898+
$key = $this->getIpKey();
899+
if ($key === 'HTTP_X_FORWARDED_FOR') {
900+
$ip = \explode(',', $_SERVER[$key], 2)[0];
901+
$ip = \trim($ip);
902+
return $ip;
903+
}
904+
return $_SERVER[$key];
899905
}
900906

901907
/**

tests/RequestTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ public function testIpKey() : void
698698
{
699699
$this->request->setIpKey('HTTP_X_FORWARDED_FOR');
700700
self::assertSame('45.181.102.88', $this->request->getIp());
701+
$_SERVER['HTTP_X_FORWARDED_FOR'] = '203.0.113.195, 2001:db8:85a3:8d3:1319:8a2e:370:7348';
702+
self::assertSame('203.0.113.195', $this->request->getIp());
701703
}
702704

703705
public function testIpKeyException() : void

0 commit comments

Comments
 (0)