Magento 2에서 "$_SERVER Superglobal의 직접 사용 감지됨"을 수정하는 방법

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