8

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

4 Answers 4

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
    – naitsirch
    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
    Mar 8, 2020 at 18:43
10

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

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
    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 Jun 12, 2021 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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