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"]
Related
I have nginx and php7.2-fpm installed on single Debian machine. nginx forwards requests to php through linux socks. I use yii2 which has request->trustedHosts setting. I need to add my local nginx there to be able to get x-forward-proto and other secure headers set.
Which address do I need to set up in trustedHosts? Ipv4 and Ipv6 cidr loopbacks do not help.
I just don't understand what network interface nginx uses to proxy request - loopback (127.0.0.1), some internal ip, external (internet) ip? So what is the default outbound ip nginx select to bind (should I use proxy_bind to change it to 127.0.0.1)?
Thanks in advance.
request->trustedHosts setting is checked against ip address provided by $_SERVER["REMOTE_ADDR"]. So check the contents of this variable.
If your machine is behind a proxy, $_SERVER["REMOTE_ADDR"] should contain proxy's ip. Try adding that ip into trustedHosts array.
(if there is no proxy and your machine is directly connected to the internet, it seems that it makes no sense to use trustedHosts. In that case $_SERVER["REMOTE_ADDR"] would contain client's ip, but you should NOT trust any client by adding "0.0.0.0/0" into trustedHosts)
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
echo $_SERVER['SERVER_ADDR'] always yields 192.168.1.142, which is the IP address of my server within my home network. I can access the website using my external IP since I've set up the port forward on my router, but $_SERVER['SERVER_ADDR'] will only return the internal IP, no matter what I try.
How can I make $_SERVER['SERVER_ADDR'] return my external IP?
You can use file_get_contents function for this. You just need to find a host which can return your ip address
$externalContent = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: ([\[\]:.[0-9a-fA-F]+)</', $externalContent, $myIp);
echo $myexternalIp = $myIp[1];
Add a second IP to your server (ifconfig eth0:1 192.168.1.143) and use that in your router's port forwarding rules. That way, external accesses will be coming in through that address instead and you can tell the difference between the two.
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