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.
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