I want to show remote address in php through global keyword and global variable , also I want to access $user_ip inside function show_ip().
<?php
$user_ip = $_SERVER['REMOTE_ADDR'];
function show_ip(){
global $user_ip;
echo "Your ip address is".$user_ip;
}
show_ip();
?>
I should get output of 127.0.0.1 where as I am getting ::1
::1 is IPv6 equivalent of 127.0.0.1. Since you are running this on local computer, the browser is able to connect over IPv6 instead of IPv4, therefore surprising you.
::1 (IPv6) is the "same" as 127.0.0.1 (IPv4). If you get ::1 this indicates that your browser accesses your local webserver using IPv6.
Related
I want to get the visitor's(users) ip address, for which i have tried like below:
$remoteAddr = Mage::helper('core/http')->getRemoteAddr(true);
echo "<pre>"; print_r($remoteAddr); echo "</pre>";
Im running on Xampp in windows and the above code returns output like below which is not even like ::1 (Tried in all browsers too!!)
Is the code right in magento.
I found out the below stackposts to see the fix why localhost returns ::1 which is equivalent of ipV6 address
Should a MAMP return ::1 as IP on localhost?
I did change the apache2/httpd.conf
Listen 80
Make it look like this:
Listen 127.0.0.1:80
But it still displays the same.
Is it right in localhost?
I am trying to get current users ip address using {$_SERVER['REMOTE_ADDR']} or {getenv('REMOTE_ADDR')}, in both of the cases i am getting ::1 .
I am using php 7.1.1, can any one help how i can get current ip address of current user.
If your using your local server it will always be ::1 or the local ip address.
$_SERVER['REMOTE_ADDR']
will return the ip when its on a server outside of your localnetwork.
$_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] look to Get the client IP address using PHP
I'm calling $_SERVER["REMOTE_ADDR"] and it returns '::1'
Any ideas why am I getting this strange output? How to get a proper ip?
::1 is the IPv6 equivalent of 127.0.0.1 which is the IP address of your local (loopback) interface.
More information here: http://en.wikipedia.org/wiki/Localhost
How to get a proper ip?
You can disable IPv6 in your webserver to only serve IPv4 hosts. This way, you will only get propers IP adresses in $_SERVER["REMOTE_ADDR"]
I write the following script to show me the host IP but its isn't working.
I'm using XAMPP on windows 7.
<?php
//Gets the IP address
$ip = getenv("REMOTE_ADDR") ;
Echo "Your IP is " . $ip;
?>
It always shows Your IP is ::1 .
Totally unexpected how should I fix it ?
::1 and 127.0.0.1 are both ip addresses of your local machine.
It is working as intended, that is the IPv6 loopback address (::1). If you want it to show up as 127.0.0.1 then disable IPv6 on your computer.
The REMOTE_ADDR key isn't supposed to be the host IP, it's the client's IP. You probably want $_SERVER['SERVER_ADDR']. However, I'm pretty sure they're the same in this case since you are connecting from localhost.
For further info:
phpinfo();
... or
print_r($_SERVER);
I am trying to write a small script that prints ip of the client. I am trying to run it on my localhost, but I am not able to get the IP address. Instead I get something like ::1.
Here's the code:
<?php echo $_SERVER['REMOTE_ADDR'];?>
Why this is not working as expected?
::1
Is the loopback adress (127.0.0.1 in IPv4) in IPv6.
This is the expected behaviour. If you use Firefox, you can navigate to about:config, search for disableIPv6 and set it to true. You'll then see 127.0.0.1.
So yes, it's working.
It is working correctly. That is your IP addresses... the IPv6 version of it. If you had connected over IPv4 it would have shown as 127.0.0.1.
These are loopback addresses. They allow you to connect to your own computer without using an actual network interface. See http://en.wikipedia.org/wiki/Loopback#Virtual_network_interface