1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\HTTP\PhpEnvironment;
use Magento\Framework\App\RequestInterface;
/**
* Library for working with server ip address
*/
class ServerAddress
{
/**
* Request object
*
* @var RequestInterface
*/
protected $request;
/**
* @param RequestInterface $httpRequest
*/
public function __construct(RequestInterface $httpRequest)
{
$this->request = $httpRequest;
}
/**
* Retrieve Server IP address
*
* @param bool $ipToLong converting IP to long format
* @return string IPv4|long
*/
public function getServerAddress($ipToLong = false)
{
$address = $this->request->getServer('SERVER_ADDR');
if (!$address) {
return false;
}
return $ipToLong ? ip2long($address) : $address;
}
}