Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    Get IP address of visitor in magento 2

    In this blog you will see how to get IP address of visitor in magento 2.

    namespace Webkul\Module\Controller\DemoController;
    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;
        public function execute()
            $ip = $this->remoteAddress->getRemoteAddress();
            echo "Visitor's IP = ".$ip;

    This will display the IP address of the user when this controller is hit.

    That’s it.

    Happy coding 🙂

    . . .

    Comments (2)

    Add Your Comment

  • thanhpv
    How to get IP address when I access from cmd? I use this function but it’s give me no result
    • Paul Dutta
      Can you please share what you wrote in cmd ?
  • Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again