34

I need to retrieve a user's IP. My localhost is returning ::1 as the IP. I am curious, if this is an expected behaviour? I'm running MAMP on Snow Leopard.

1

5 Answers 5

38

Same question, and found a valid solution, tested, works well. I wanted to have the ip addy - of 127.0.0.1 as well instead of having to accept the the ::1 and debate the whole IPv4/6 issues. So, i trolled for a few moments and fell onto a 2008 comment made by @Brad - here: http://board.issociate.de/thread/489575/SERVERquotREMOTEADDRquot-returning-1.html

Summarizing - (on Mac OS - Mountain Lion in particular)

sudo vi /etc/apache2/httpd.conf

Find where your apache is 'listen'-ing to the ips/ports, etc... Typically this will be a line looking like this

Listen 80

Make it look like this:

Listen 127.0.0.1:80

Resave it. Restart Apache. Voila!

Now $_SERVER[REMOTE_ADDR] will look like this 127.0.0.1.

Hope it helps someone.

5
  • 1
    "Listen 127.0.0.1:80" you mean :) Mar 13, 2013 at 15:47
  • 3
    This way you are disabling IPv6 support for Apache. You can fix it another way: add 127.0.0.1 localhost to hosts file, so IPv4 resolution is priorized over IPv6 for localhost ;) Dec 4, 2013 at 1:52
  • This doesn't seem to do anything o El Capitan with MAMP 3.5 Aug 26, 2016 at 14:47
  • @ÁxelCostasPena I've got that on the first line of hosts, still get ::1 in PHP. Aug 26, 2016 at 14:49
  • @DesignbyAdrian what IP shows when you ping localhost? Sep 2, 2016 at 10:22
31

::1 is the IPv6 equivalent of IPv4 127.0.0.1 address.

You can convert IPv6 address to IPv4 address using function inet_ntop() as Adam, said.

http://php.net/manual/en/function.inet-ntop.php

6
  • 1
    I believe you can convert between IPv4 and IPv6 using inet_ntop Sep 13, 2010 at 10:19
  • @adam, yes, you are right. @Mild: if you are satisfied with my answer, please accept ;] Sep 13, 2010 at 10:21
  • 1
    $ip=inet_ntop($_SERVER['REMOTE_ADDR']); is returning Warning: inet_ntop() [function.inet-ntop]: Invalid in_addr value in /Applications/MAMP/htdocs/createstudios/wp-content/themes/createstudios/page.php on line 18
    – Mild Fuzz
    Sep 13, 2010 at 10:26
  • Was your PHP library built with IPv6 support? Sep 13, 2010 at 10:28
  • and, ultimately, does it matter? Are these values good enough?
    – Mild Fuzz
    Sep 13, 2010 at 10:40
6

Modifying the /etc/apache2/httpd.conf or even /private/etc/hosts isn't the solution. It's returning ::1 because it's the IPv6 equivalent to the old (but gold) IPv4's 127.0.0.1. As it's defined in the /privates/etc/hosts, whenever you access http://localhost it assumes IPv6 ::1. So it's the expected behavior.

A workaround would be to access http://127.0.0.1 directly. It'll work just fine. But remember that IPv6 availability is increasing. You could make sure your server (after deployment) will only respond to IPv4 but in the mid term, if your site/web app does not handle both IPv4 and IPv6 properly, there's a great chance something will go wrong within 1/2 years. Or sooner, we never know.

2

for who has not reslove this issue it sounds that the hosts file is missing or the line

127.0.0.1       localhost   

in etc\hosts LINUX

in /private/etc/hosts MAC devices

in \%WINDIR%\system32\drivers\etc\hosts WINDOWS xp/vista/7/8/

whene the file hosts is removed or missed or the line too the IPV6 will be set by default ...

0

You have to disable IPv6 support in your OS for apache to stop showing these IPv6 format addresses, when you want the IPv4 format. I disabled IPv6 support and its working as expected now.

I recently ran into this issue, though i don't ever remember enabling ipv6 support on my Windows 7 system, could be done by Microsoft in one of their updates (maybe).

1
  • I believe that IPv6 has been enabled by default since Windows XP SP2 or even earlier. Jul 9, 2013 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.