In magento 1 it
$user_agent = Mage::helper('core/http')->getHttpUserAgent();
$remote_ip = Mage::helper('core/http')->getRemoteAddr();
Are there any similar features in magento 2 ?
You can use this below code :
protected $httpHeader;
protected $remoteIp;
public function __construct(
\Magento\Framework\HTTP\Header $httpHeader,
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteIp
)
{
$this->httpHeader = $httpHeader;
$this->remoteIp = $remoteIp;
}
public function execute(){
$userAgent = $this->httpHeader->getHttpUserAgent();
$remote_ip = $this->remoteIp->getRemoteAddress();
}
Try below code:
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
class DemoClass extends Action {
private $remoteAddress;
public function __construct(
Context $context,
RemoteAddress $remoteAddress
) {
$this->remoteAddress = $remoteAddress;
parent::__construct($context);
}
public function execute()
{
$ip = $this->remoteAddress->getRemoteAddress();
echo "Visitor's IP = ".$ip;
}
}