How to Get IP Address of Magento 2 Visitor

In this blog you will perceive how to get IP address of guest in magento 2.


<?php

namespace\Module\Controller\DemoController;

use Magento\Framework\App\Action\Action;

use Magento\Framework\App\Action\Context;

use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;

class DemoClass expands Activity {

    confidential $remoteAddress;


    public capability __construct(

        Setting $context,

        RemoteAddress $remoteAddress

    ) {

        $this->remoteAddress = $remoteAddress;

        parent::__construct($context);

    }

    public capability execute()

    {

        $ip = $this->remoteAddress->getRemoteAddress();

        reverberation "Guest's IP = ".$ip;

    }

}

This will show the IP address of the client when this regulator is hit.


References :























Comments

Popular posts from this blog

FAQ Schema Generator

10 Native Advertising Statistics You Need to Know [2023]

Solved: Error 1273 Unknown Collation utf8mb4_0900_ai_ci in MySQL