Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IP Address of the machine in PHP gives ::1 but why?

Tags:

php

ip-address

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.

like image 785
John Avatar asked May 09 '12 13:05

John


People also ask

What does :: 1 means in 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.

How can I get IP address in PHP?

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.

How can I get static IP in PHP?

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.

Which of the following server array elements contains the IP address from which the user is viewing the current page?

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


2 Answers

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

like image 95
Quentin Avatar answered Nov 15 '22 19:11

Quentin


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

like image 41
Yigit Tanriverdi Avatar answered Nov 15 '22 19:11

Yigit Tanriverdi