как исправить «Обнаружено прямое использование $_SERVER Superglobal» в Magento 2

magento.stackexchange https://magento.stackexchange.com/questions/107955

  •  29-09-2020
  •  | 
  •  

Вопрос

У меня возникла проблема с утверждением модуля.и отчет о проверке соединения magento генерирует следующее сообщение

Обнаружено прямое использование $_SERVER Superglobal

как я могу использовать альтернативу для

$_SERVER['REMOTE_ADDR']

в Мадженто 2.

Это было полезно?

Решение

Использование вспомогательного класса, расширяющего /Magento/Framework/App/Helper/AbstractHelper класс вы можете использовать следующее:

$this->_remoteAddress

Он установлен в AbstractHelper конструктор класса:

public function __construct(Context $context)
{
    $this->_moduleManager = $context->getModuleManager();
    $this->_logger = $context->getLogger();
    $this->_request = $context->getRequest();
    $this->_urlBuilder = $context->getUrlBuilder();
    $this->_httpHeader = $context->getHttpHeader();
    $this->_eventManager = $context->getEventManager();
    $this->_remoteAddress = $context->getRemoteAddress();
    $this->_cacheConfig = $context->getCacheConfig();
    $this->urlEncoder = $context->getUrlEncoder();
    $this->urlDecoder = $context->getUrlDecoder();
    $this->scopeConfig = $context->getScopeConfig();
}

Это происходит от /Magento/Framework/App/Helper/Context.php сорт:

/**
 * @return \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
 */
public function getRemoteAddress()
{
    return $this->_remoteAddress;
}

И это пример \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress :

/**
 * Retrieve Client Remote Address
 *
 * @param bool $ipToLong converting IP to long format
 * @return string IPv4|long
 */
public function getRemoteAddress($ipToLong = false)
{
    if ($this->remoteAddress === null) {
        foreach ($this->alternativeHeaders as $var) {
            if ($this->request->getServer($var, false)) {
                $this->remoteAddress = $this->request->getServer($var);
                break;
            }
        }

        if (!$this->remoteAddress) {
            $this->remoteAddress = $this->request->getServer('REMOTE_ADDR');
        }
    }

    if (!$this->remoteAddress) {
        return false;
    }

    return $ipToLong ? ip2long($this->remoteAddress) : $this->remoteAddress;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top