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.
The simple answer is that: ::1 is the compressed format of IPV6 loopback address 0:0:0:0:0:0:0:1 . It is the equivalent of the IPV4 address 127.0. 0.1.
Using getenv() function: To get the IP Address,we use getenv(“REMOTE_ADDR”) command. The getenv() function in PHP is used for retrieval of values of an environment variable in PHP. It is used to return the value of a specific environment variable.
The simplest way is to use $_SERVER with 'REMOTE_ADDR', it will return the user's IP address who is currently viewing the page. Example using ['REMOTE_ADDR'] to identify the server's IP address in PHP.
$_SERVER['REMOTE_ADDR'] - This contains the real IP address of the client. That is the most reliable value you can find from the user. 2. $_SERVER['REMOTE_HOST'] - This will fetch the host name from which the user is viewing the current page.
::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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With