ServerAddress.php 946 B

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