Skip to content

Commit

Permalink
add trim to make clientIp method always return string
Browse files Browse the repository at this point in the history
  • Loading branch information
melbahja committed Jun 14, 2018
1 parent ccbd3ac commit e080ce0
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/Http/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,37 +584,37 @@ public function session(Session $session = null)
*/
public function clientIp()
{
if ($this->trustProxy) {
$forwarded = $this->getEnv('HTTP_X_FORWARDED_FOR');
if ($forwarded) {
$addresses = array_map('trim', explode(',', $forwarded));
$trusted = (count($this->trustedProxies) > 0);
$n = count($addresses);

if ($trusted) {
for ($i = 1; $i < $n; $i++) {
if (in_array($addresses[$i], $this->trustedProxies) === false) {
$trusted = false;
break;
}
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_FOR')) {

$addresses = array_map('trim', explode(',', $this->getEnv('HTTP_X_FORWARDED_FOR')));
$trusted = (count($this->trustedProxies) > 0);
$n = count($addresses);

if ($trusted) {
for ($i = 1; $i < $n; $i++) {
if (in_array($addresses[$i], $this->trustedProxies) === false) {
$trusted = false;
break;
}
}

if ($trusted) {
return $addresses[0];
}

return $addresses[$n - 1];
}

if ($this->getEnv('HTTP_X_REAL_IP')) {
return $this->getEnv('HTTP_X_REAL_IP');
} elseif ($this->getEnv('HTTP_CLIENT_IP')) {
return $this->getEnv('HTTP_CLIENT_IP');
if ($trusted) {
return $addresses[0];
}

return $addresses[$n - 1];
}

if ($this->trustProxy && $this->getEnv('HTTP_X_REAL_IP')) {
$ipaddr = $this->getEnv('HTTP_X_REAL_IP');
} elseif ($this->trustProxy && $this->getEnv('HTTP_CLIENT_IP')) {
$ipaddr = $this->getEnv('HTTP_CLIENT_IP');
} else {
$ipaddr = $this->getEnv('REMOTE_ADDR');
}

return $this->getEnv('REMOTE_ADDR', '');
return trim($ipaddr);
}

/**
Expand Down

0 comments on commit e080ce0

Please sign in to comment.