Skip to content

Commit 487ec6d

Browse files
authored
Merge pull request #7030 from kenjis/fix-request-getIPaddress-cli
fix: Request::getIPaddress() causes error on CLI
2 parents f732fa3 + 4454380 commit 487ec6d

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

system/HTTP/RequestTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ trait RequestTrait
4444
/**
4545
* Gets the user's IP address.
4646
*
47-
* @return string IP address if it can be detected, or empty string.
47+
* @return string IP address if it can be detected.
4848
* If the IP address is not a valid IP address,
4949
* then will return '0.0.0.0'.
5050
*/
@@ -73,6 +73,11 @@ public function getIPAddress(): string
7373

7474
$this->ipAddress = $this->getServer('REMOTE_ADDR');
7575

76+
// If this is a CLI request, $this->ipAddress is null.
77+
if ($this->ipAddress === null) {
78+
return $this->ipAddress = '0.0.0.0';
79+
}
80+
7681
if ($proxyIPs) {
7782
// @TODO Extract all this IP address logic to another class.
7883
foreach ($proxyIPs as $proxyIP => $header) {
@@ -151,7 +156,7 @@ public function getIPAddress(): string
151156
return $this->ipAddress = '0.0.0.0';
152157
}
153158

154-
return empty($this->ipAddress) ? '' : $this->ipAddress;
159+
return $this->ipAddress;
155160
}
156161

157162
/**

user_guide_src/source/incoming/request.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Class Reference
2222
2323
.. php:method:: getIPAddress()
2424
25-
:returns: The user's IP Address, if it can be detected, or null. If the IP address
26-
is not a valid IP address, then will return 0.0.0.0
25+
:returns: The user's IP Address, if it can be detected. If the IP address
26+
is not a valid IP address, then will return 0.0.0.0.
2727
:rtype: string
2828

2929
Returns the IP address for the current user. If the IP address is not valid, the method

0 commit comments

Comments
 (0)