Request Quote

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 🙂

. . .


Add Your Comment

Be the first to comment.

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