كيفية إصلاح "تم اكتشاف الاستخدام المباشر لـ $_SERVER Superglobal" في Magento 2
-
29-09-2020 - |
سؤال
أواجه مشكلة في الموافقة على الوحدة النمطية.ويقوم تقرير التحقق من صحة Magento Connect بإنشاء الرسالة التالية
تم اكتشاف الاستخدام المباشر لـ $_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;
}
لا تنتمي إلى magento.stackexchange