Skip to content

Commit 1e596fd

Browse files
committed
Allow set custom key to get IP address from _SERVER
1 parent 8ce19ad commit 1e596fd

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/Debug/HTTPCollector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ protected function renderRequest() : string
7777
return '<p>A Request instance has not been set on this collector.</p>';
7878
}
7979
\ob_start(); ?>
80-
<p title="REMOTE_ADDR"><strong>IP:</strong> <?= $this->request->getIp() ?></p>
80+
<p>
81+
<strong>IP:</strong> <?= $this->request->getIp() ?>
82+
<span class="opaque">(<?= $this->request->getIpKey() ?>)</span>
83+
</p>
8184
<p><strong>Is Secure:</strong> <?= $this->request->isSecure() ? 'Yes' : 'No' ?></p>
8285
<p><strong>Protocol:</strong> <?= $this->request->getProtocol() ?></p>
8386
<p><strong>Method:</strong> <?= $this->request->getMethod() ?></p>

src/Request.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Request extends Message implements RequestInterface
7171
*/
7272
protected bool $isSecure;
7373
protected int $jsonFlags = 0;
74+
protected string $ipKey = 'REMOTE_ADDR';
7475

7576
/**
7677
* Request constructor.
@@ -822,7 +823,37 @@ public function getId() : ?string
822823
*/
823824
public function getIp() : string
824825
{
825-
return $_SERVER['REMOTE_ADDR'];
826+
return $_SERVER[$this->getIpKey()];
827+
}
828+
829+
/**
830+
* Set the key used to get the IP address from the superglobal $_SERVER.
831+
*
832+
* @param string $ipKey
833+
*
834+
* @throws InvalidArgumentException for IP key not set in the $_SERVER var
835+
*
836+
* @return static
837+
*/
838+
public function setIpKey(string $ipKey) : static
839+
{
840+
if (!isset($_SERVER[$ipKey])) {
841+
throw new InvalidArgumentException(
842+
'The IP Key "' . $ipKey . '" is not set in the $_SERVER'
843+
);
844+
}
845+
$this->ipKey = $ipKey;
846+
return $this;
847+
}
848+
849+
/**
850+
* Get the key used to get the IP address from the superglobal $_SERVER.
851+
*
852+
* @return string
853+
*/
854+
public function getIpKey() : string
855+
{
856+
return $this->ipKey;
826857
}
827858

828859
#[Override]

tests/RequestMock.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class RequestMock extends \Framework\HTTP\Request
3131
'HTTP_HOST' => 'domain.tld',
3232
'HTTP_REFERER' => 'http://domain.tld/contact.html',
3333
'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0',
34+
'HTTP_X_FORWARDED_FOR' => '45.167.104.99',
3435
'HTTP_X_REQUEST_ID' => 'abc123',
3536
'HTTP_X_REQUESTED_WITH' => 'XMLHTTPREQUEST',
3637
'REMOTE_ADDR' => '192.168.1.100',

tests/RequestTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public function testToString() : void
266266
'Host: domain.tld',
267267
'Referer: http://domain.tld/contact.html',
268268
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0',
269+
'X-Forwarded-For: 45.167.104.99',
269270
'X-Request-ID: abc123',
270271
'X-Requested-With: XMLHTTPREQUEST',
271272
];
@@ -642,6 +643,7 @@ public function testHeaders() : void
642643
'host' => 'domain.tld',
643644
'referer' => 'http://domain.tld/contact.html',
644645
'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0',
646+
'x-forwarded-for' => '45.167.104.99',
645647
'x-request-id' => 'abc123',
646648
'x-requested-with' => 'XMLHTTPREQUEST',
647649
], $this->request->getHeaders());
@@ -652,6 +654,21 @@ public function testIP() : void
652654
self::assertSame('192.168.1.100', $this->request->getIp());
653655
}
654656

657+
public function testIpKey() : void
658+
{
659+
$this->request->setIpKey('HTTP_X_FORWARDED_FOR');
660+
self::assertSame('45.167.104.99', $this->request->getIp());
661+
}
662+
663+
public function testIpKeyException() : void
664+
{
665+
$this->expectException(\InvalidArgumentException::class);
666+
$this->expectExceptionMessage(
667+
'The IP Key "HTTP_CF_CONNECTING_IP" is not set in the $_SERVER'
668+
);
669+
$this->request->setIpKey('HTTP_CF_CONNECTING_IP');
670+
}
671+
655672
public function testIsAjax() : void
656673
{
657674
self::assertTrue($this->request->isAjax());

0 commit comments

Comments
 (0)