PHP $_SERVER['REMOTE_ADDR'] shows IPv6
The server is then accepting connections on an IPv6 socket. Some operating systems can do both IPv4 and IPv6 on an IPv6 socket. When that happens the IPv6 address will look like ::ffff:192.0.2.123
or ::ffff:c000:027b
which is the same address but written in hexadecimal.
If you see IPv6 addresses like 2a00:8640:1::224:36ff:feef:1d89
then your webserver really is reachable over IPv6 :-)
Anyway, to convert everything back to a canonical form you can use something like:
// Known prefix
$v4mapped_prefix_hex = '00000000000000000000ffff';
$v4mapped_prefix_bin = pack("H*", $v4mapped_prefix_hex);
// Or more readable when using PHP >= 5.4
# $v4mapped_prefix_bin = hex2bin($v4mapped_prefix_hex);
// Parse
$addr = $_SERVER['REMOTE_ADDR'];
$addr_bin = inet_pton($addr);
if( $addr_bin === FALSE ) {
// Unparsable? How did they connect?!?
die('Invalid IP address');
}
// Check prefix
if( substr($addr_bin, 0, strlen($v4mapped_prefix_bin)) == $v4mapped_prefix_bin) {
// Strip prefix
$addr_bin = substr($addr_bin, strlen($v4mapped_prefix_bin));
}
// Convert back to printable address in canonical form
$addr = inet_ntop($addr_bin);
Using this code, when you input one of the following:
::ffff:192.000.002.123
::ffff:192.0.2.123
0000:0000:0000:0000:0000:ffff:c000:027b
::ffff:c000:027b
::ffff:c000:27b
192.000.002.123
192.0.2.123
you always get the canonical IPv4 address 192.0.2.123
as output.
And of course IPv6 addresses get returned as canonical IPv6 addresses: 2a00:8640:0001:0000:0224:36ff:feef:1d89
becomes 2a00:8640:1::224:36ff:feef:1d89
etc.
REMOTE_ADDR and IPv6 in PHP
The REMOTE_ADDR key is set by the web server, not PHP. If the web server listens on v6 and the user connects that way, it'll be a v6 address
IPV6 notation of $_SERVER['REMOTE_ADDR']
If you use inet_pton()
first, and then convert it back to a string with inet_ntop()
you should have a consistent string representation. I wouldn't rely on the input to be consistent...
How show IPv4 only with REMOTE_ADDR in PHP?
$_SERVER['REMOTE_ADDR']
always contains the address of the visitor. If it contains an IPv6 address then the visitor used IPv6 and there is no IPv4 address. And vice versa of course. These days you have to be able to deal with both.
Some visitors will have only IPv4, some will have only IPv6 and some will have both. The browser decides what is available and what it will use, and that's all you'll see. Note that a browser that has both might even switch between IPv4 and IPv6 between requests if it deems that necessary for good connectivity.
IP Address of the machine in PHP gives ::1 but why?
::1
is the actual IP. It is an ipv6 loopback address (i.e. localhost). If you were using ipv4 it would be 127.0.0.1
.
If you want to get a different IP address, then you'll need to connect to the server through a different network interface.
$_SERVER['REMOTE_ADDR'] not giving the right ip address
<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />
Don't do that. Get the request from $_SERVER
when the form is submitted. Getting it when the form is generated and storing it in the form just gives people the opportunity to change it.
Does this IP address normally happen when I use it in a localhost (XAMPP)?
Yes. Getting the local IP (IPv6) address is normal when you request a page from localhost.
$_SERVER['REMOTE_ADDR'] is not returning ip address
::1
is the actual IP. It is an ipv6 address (i.e. localhost). If you were using ipv4 it would be 127.0.0.1.
If you want to get a different IP address, then you'll need to connect to the server through a different network interface.
Related Topics
Gmail API: 400 Bad Request When Trying to Send an Email (PHP Code)
Adding Additional Persist Calls to Preupdate Call in Symfony 2.1
Create Unique Poll/Vote/Survey in PHP
Change Pay Button on Checkout Based on Woocommerce Chosen Payment Method
Remove Duplicates from an Array Based on Object Property
Why Composer Can Not Find My Brand New Package on Packagist.Org
Using Mod_Rewrite with Xampp and Windows 7 - 64 Bit
How to Remove Numbers from a String with Regex
How to Stop Curl from Using 100 Continue
How to Get Month from Date in MySQL
Php: Change Color of Text Based on $Value
PHP Passing Parameters via Url
Troubleshooting "Unexpected T_Echo" in Ternary Operator Statement
Mysqli Join Tables from 2 Different Databases
Add a Shipping to an Order Programmatically in Woocommerce 3