9

How could I get the current customer's IP address?

5 Answers 5

3

Please try following

if(!empty($_SERVER['HTTP_CLIENT_IP'])){

    //ip from share internet
    $ip = $_SERVER['HTTP_CLIENT_IP'];

}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){

    //ip pass from proxy
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

}else{

    $ip = $_SERVER['REMOTE_ADDR'];

}

return $ip;
2
  • 2
    This is not the Magento way. I'd prefer the way described by @waqar-ali
    – stollr
    Commented Feb 14, 2020 at 9:49
  • Note that Magento overwrites global vars contents in some (if not all) contexts, so this solution may not work.
    – Klesun
    Commented Mar 8, 2020 at 18:43
12

Try This\

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;

class Index 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;
    }
}

Source Source

2
  • How can I add this to the contact form in Magento2?
    – Mike
    Commented Jun 21, 2022 at 13:54
  • Most Probably you have to use plugin or preference to Contact form save class
    – Waqar Ali
    Commented Jun 22, 2022 at 8:57
0

Use this block of code where you want to get ip:

function getUserIpAddr(){
        if(!empty($_SERVER['HTTP_CLIENT_IP'])){
            //ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
            //ip pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }else{
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

echo 'IP : '.getUserIpAddr();

Hope it works

0

For the Magento 2 way, you also need to add into the di.xml any headers you want to check or it isn't equivalent @Chriag. It will loop through each header and break if it finds one, and if not return remote_addr

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\HTTP\PhpEnvironment\RemoteAddress">
    <arguments>
        <argument name="alternativeHeaders" xsi:type="array">
            <item name="http_client_ip" xsi:type="string">HTTP_CLIENT_IP</item>
            <item name="http_x_forwarded_for" xsi:type="string">HTTP_X_FORWARDED_FOR</item>
        </argument>
    </arguments>
</type>
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;

class Index 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;
    }
}
2
  • is there any security issue in using this method ?
    – Ashar Riaz
    Commented Jun 11, 2021 at 10:59
  • @AsharRiaz no more than any other method, it is just magento doing the same thing as the other methods, but just through its framework and magento cleans it up a little more Commented Jun 12, 2021 at 17:30
0
$objctManager = \Magento\Framework\App\ObjectManager::getInstance();
$remote = $objctManager->get('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');
echo $remote->getRemoteAddress();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.