IP address of customer in Magento - 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

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.

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.

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)

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]);

Related

getaddrinfo with flag AI_NUMERICHOST

Please tell me something I can't understand. There is a function getaddrinfo() and there is a flag AI_NUMERICHOST. MSDN says that in getaddrinfo() with this flag, you need to send the numeric value of the IP address, and not the domain name. But why?
I already have an IP address, why should I ask DNS for an IP address?
getaddrinfo() outputs sockaddr_... structs (sockaddr_in or sockaddr_in6) for the requested host/service. It is not just about IP addresses, it is also about other things, like socket types, service ports, etc, depending on your input and hint values.
So, if you already have an IP address in a string format, you can have getaddrinfo() parse that string for you (specifying AI_NUMERICHOST to avoid DNS) into a binary format in the output sockaddr_..., as well as fill in other sockaddr_... fields at the same time.

Ruby: How to convert ipv6 uo address to ipv4 ip address?

It is easy to convert ipv4 to ipv6 with ipv4_mapped. But how about the other around?
I did not find a way to do that in IpAddr documentation.
Firstly, it's important to understand that it's impossible to convert most IPv6 addresses to IPv4 addresses (for several reasons, the most obvious being that there's no way to fit a 128-bit number into 32 bits). The only IPv6 addresses that can be converted to IPv4 are ones that are mapped from IPv4 address (such as those produced by IPAddr#ipv4_mapped). For that, we have IPAddr#native:
require "ipaddr"
ip = IPAddr.new("192.168.2.128")
ipv6 = ip.ipv4_mapped
puts ipv6.native
# => 192.168.2.128

ipv6calc outputs wrong address when converting from ipv4 to ipv6?

Having a strange issue while trying to convert an ipv4 list file to ipv6:
ipv6calc -q --action conv6to4 --in ipv4 1.1.23.1 --out ipv6
2002:101:1701::
Trying to validate that result is correct, I used some online converters and it seems that 1.1.23.1 is 2002:0:0:0:0:0:101:1701 (or else 2002::101:1701).
So the last "::" should be removed & 2002 should have extra ":".
I really don't want to use sed/awk commands in order to manipulate this result, so the questions are:
is there alternative cmd/linux SW?
is this somehow fixed inside ipv6 calc, am I doing something wrong?
Thanks
This is the correct 6to4 address. A 6to4 subnet is on the format 2002:IP4_HI:IP4_LO::/48. IP4_HI is the top 16 bits of the IPv4 address, while IP4_LO is the low 16 bits of the address.
For example, the IPv4 address 1.2.3.4 gives you the 6to4 subnet 2002:0102:0304::/48.
See 6to4 address block allocation for more details.
A different question is whether this is actually the address you want? There are other ways to map IPv4 addresses to IPv6 addresses. For example, there are IPv4-mapped IPv6 addresses, which are typically written as ::ffff:1.2.3.4.
The address format you need depends on what you are going to use it for.

'getaddrinfo()' function, it returns IP address from 'etc\hosts' file only ...?

I have dual stack Windows m/c, with IPv4 and IPv6 address.
The etc\hosts contains only IPv4 address of that hostname.
So whenever I call getaddrinfo() function, it returns IP address from etc\hosts file (i.e IPv4 only not IPv6 address).
ipconfig command shows both IPv4 as well as IPv6 address.
How to configure getaddrinfo() function to always return IPs from DNS rather than etc\hosts file ?
Any other way to do this ?
Name resolution is entirely independent from address configuration. Unless the name resolver learns of the IPv6 address (by inclusion in the hosts file, or a DNS entry, or mDNS, or...), it cannot return the address.

Find internal IP address with BASH

I am already aware of many ways of getting your internal IP (ifconfig, ip addr, /etc/hosts, etc), but I am trying to write a bash script that will always return the internal IP. The problem is, many one-liners (/sbin/ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}') can return multiple IPs, and I need to distinguish the internal one manually.
I suspect that to the computer, there is no difference between and an external IP and an internal IP, and thus no 100%, guaranteed way to get the right IP.
The end result is that this script will return the internal IP, no matter if its a 192 address or a 204 address, etc.
Thanks in advance.
"hostname -i" should hopefully give you the same result
As others have mentioned, a machine is not really guaranteed, or even likely, to have a single IP address. I'm not sure exactly what you mean by "internal IP"; sometimes this can mean "IP address on the local network", i.e. the interface which connects to a NAT-enabled firewall.
I'm thinking that the best way to do this is to connect to a host on the network you want and use the address from which that connection originates. This will be the interface which the machine normally uses to connect to that network. The user Unkwntech had the same idea on this thread. The code below is just taken from that answer.
I don't know if this really qualifies as a "bash" solution, since it's just an inline Python script, but anyway this will get you the local ip address used to reach google.com. So this will give you the IP address of whichever interface the machine uses to reach Internet hosts.
$ python -c 'import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("google.com", 80))
print s.getsockname()[0]'
A more bash-y solution might use tracepath or some similar utility.
Systems can have multiple private IPs too though. You would have to limit your searching on IPs to private IPs. 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16.
Within the RFC 1918 private address spaces, a machine could conceivably have every address in the 10/8 range, the 172.16/12 range, and the 192.168/16 range, for a total of 17891328 IP addresses, and all of them would be legal "internal" IPs.
Oh yes, don't forget IPv6 :) 2^64 possible addresses per network for a single machine, which might participate in multiple networks.
This isn't exactly academic, either: it is quite common for VMWare, VirtualBox, QEMU, etc. host systems to have multiple RFC 1918 addresses assigned; one for the 'usual use', and one that is used specifically to communicate with guest operating systems. Or routers / firewalls, they might have a dozen internal IPs specifically to subnet a network for access control reasons.

Resources