How could I get the current customer's IP address?
5 Answers
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;
-
2This is not the Magento way. I'd prefer the way described by @waqar-ali– stollrCommented 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.– KlesunCommented Mar 8, 2020 at 18:43
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
-
-
Most Probably you have to use plugin or preference to Contact form save class Commented Jun 22, 2022 at 8:57
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
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;
}
}
-
-
@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
$objctManager = \Magento\Framework\App\ObjectManager::getInstance();
$remote = $objctManager->get('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');
echo $remote->getRemoteAddress();