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);
Related
I am trying to fetch the ip address of my machine through php. For that I am writing the code like:
<?php echo "<br />".$_SERVER['REMOTE_ADDR'];?>
But this piece of code is not working. It is returning "::1".
Please help me how to get the actual IP Address.
::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.
If you are trying to run localhost, this answer will fix your problem. Just few changes on
apache2/httpd.conf
search all "listen" words
ex:
Listen 80
Make like this.
Listen 127.0.0.1:80
than restart your apache
$_SERVER[REMOTE_ADDR]
will show Listen 127.0.0.1
you can see answer in this detailed answer link
If you mean getting the user's IP address, you can do something like :
<?php
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
$ip=$_SERVER['REMOTE_ADDR'];
}
?>
<?php echo "<br />".$ip;?>
It will get the user's actual IP address, regardless of proxies etc.
$_SERVER['REMOTE_ADDR'] is the IP address of the client.
$_SERVER['SERVER_ADDR'] is the IP address of the server.
Reference: http://php.net/manual/en/reserved.variables.server.php
Simple answer: You are using it on local server.
Try running
function getUserIpAddr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
echo 'User Real IP - '.getUserIpAddr();
in real server. Or you can also user online php executor.
Look at the output of phpinfo(). If the address is not on that page, then the address is not available directly through PHP.
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.
I'm quite a novice at PHP.
I would like the IP address that I (myself, only. I modified my hosts file for the HotSpot shield webpage) have been given when using HotSpot shield to show up on my webpage, but unfortunately, it's echoed as 127.0.0.1. The main objective is that when I connect, the IP address that I've been set is echoed on the page. This is what code I've used:
<?php $rmt = $_SERVER["REMOTE_ADDR"]; echo "$rmt \n" ?>
The only problem is is that $rmt is 127.0.0.1. Do any of you know how to get it to display the public IP address to be displayed?
This can happen with local proxy servers; you could check out the other headers that are sent with your request by var_dump($_SERVER); and search for stuff like X-Forwarded-For or X-Real-IP
$_SERVER['REMOTE_ADDR'] is referring to the IP adress from which you connected. If you're running the server on your local machine and connecting from your local machine, it uses your local ip (127.0.0.1 is always "localhost").
In order to show a different ip you need to run it on another server (a public web hotel server preferably), or connect to your server from another machine.
I had just the same issue.
As it turns out, I was getting the proxy IP address instead of my own IP.
So I ran:
var_dump($_SERVER)
//you could also use print_r($_SERVER);
And then looked for something like this:
["HTTP_X_REAL_IP"]
Then captured it into a var like this:
$ip = getenv('HTTP_X_REAL_IP')
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 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