M.I.T.
M.I.T.

Reputation: 1042

IP address of customer in Magento

I Want IP address from which the user is viewing the current page

and i m using

echo $_SERVER['REMOTE_ADDR'];

and it show me 127.0.0.1

Upvotes: 3

Views: 22339

Answers (4)

Alessandro Conti
Alessandro Conti

Reputation: 11

If you have the machine behind a proxy it is better to use this kind of code:

$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $ip =trim($ips[count($ips) - 1]);

Upvotes: 0

PKSOFT 1985
PKSOFT 1985

Reputation: 1

You can use this one: Mage::helper('core/http')->getRemoteAddr();

This will return ip address such as '127.0.0.1'

When you use Mage::helper('core/http')->getRemoteAddr(true); it will return long (2130706433)

Upvotes: 0

Mukesh Chapagain
Mukesh Chapagain

Reputation: 25958

Here is a Magento function to get customer's IP Address:

echo Mage::helper('core/http')->getRemoteAddr();

getRemoteAddr() also accepts a boolean parameter. When false (default) it will return the IP address as a string in the common dotted-decimal notation (e.g. 192.168.0.1). When true it will return the IP in its decimal notation--a 32-bit integer. See IPv4 Address Representations to understand the formats.

Upvotes: 22

Jürgen Thelen
Jürgen Thelen

Reputation: 12727

When Magento (server) and the browser (client) are both on your computer (localhost), then it's not an error, but correct that PHP's $_SERVER['REMOTE_ADDR'] contains 127.0.0.1 (or ::1).

This is because 127.0.0.1 is the standard IPv4 loopback address for any localhost.

See section "3. Global and Other Specialized Address Blocks" of RFC 5735:

127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher-level protocol to an address anywhere within this block loops back inside the host. This is ordinarily implemented using only 127.0.0.1/32 for loopback.

Upvotes: 1

Related Questions