Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
RemoteAddress Class Reference

Public Member Functions

 __construct (RequestInterface $httpRequest, array $alternativeHeaders=[], array $trustedProxies=null)
 
 getRemoteAddress (bool $ipToLong=false)
 
 getRemoteHost ()
 

Protected Attributes

 $request
 
 $remoteAddress
 
 $alternativeHeaders
 

Detailed Description

Library for working with client ip address.

Definition at line 13 of file RemoteAddress.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( RequestInterface  $httpRequest,
array  $alternativeHeaders = [],
array  $trustedProxies = null 
)
Parameters
RequestInterface$httpRequest
array$alternativeHeaders
string[]|null$trustedProxies

Definition at line 44 of file RemoteAddress.php.

48  {
49  $this->request = $httpRequest;
50  $this->alternativeHeaders = $alternativeHeaders;
51  $this->trustedProxies = $trustedProxies;
52  }

Member Function Documentation

◆ getRemoteAddress()

getRemoteAddress ( bool  $ipToLong = false)

Retrieve Client Remote Address. If alternative headers are used and said headers allow multiple IPs it is suggested that trusted proxies is also used for more accurate IP recognition.

Parameters
bool$ipToLongconverting IP to long format
Returns
string IPv4|long

Definition at line 120 of file RemoteAddress.php.

121  {
122  if ($this->remoteAddress !== null) {
123  return $this->remoteAddress;
124  }
125 
126  $remoteAddress = $this->readAddress();
127  if (!$remoteAddress) {
128  $this->remoteAddress = false;
129 
130  return false;
131  }
132  $remoteAddress = $this->filterAddress($remoteAddress);
133 
134  if (!$remoteAddress) {
135  $this->remoteAddress = false;
136 
137  return false;
138  } else {
139  $this->remoteAddress = $remoteAddress;
140 
141  return $ipToLong ? ip2long($this->remoteAddress) : $this->remoteAddress;
142  }
143  }

◆ getRemoteHost()

getRemoteHost ( )

Returns internet host name corresponding to remote server

Returns
string|null

Definition at line 150 of file RemoteAddress.php.

151  {
152  return $this->getRemoteAddress()
153  ? gethostbyaddr($this->getRemoteAddress())
154  : null;
155  }

Field Documentation

◆ $alternativeHeaders

$alternativeHeaders
protected

Definition at line 32 of file RemoteAddress.php.

◆ $remoteAddress

$remoteAddress
protected

Definition at line 27 of file RemoteAddress.php.

◆ $request

$request
protected

Definition at line 20 of file RemoteAddress.php.


The documentation for this class was generated from the following file: