Solved

get users IP address showing ::1

Posted on 2016-09-28
8
113 Views
Last Modified: 2016-09-28
I am trying to echo a users IP address. If I use :

echo $_SERVER['REMOTE_ADDR'];

Open in new window


it outputs ::1 for some reason?
0
Comment
Question by:Black Sulfur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 26

Assisted Solution

by:-MAS
-MAS earned 100 total points
ID: 41819855
Hi,
::1 is the loopback address in IPv6
127.0.0.1 is the loopback address in IPv4
Please check this for details
http://stackoverflow.com/questions/4611418/what-is-ip-address-1
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41819866
Okay. Let me elaborate. I want to store user's IP addresses in a database every time they try login and the login fails because either the email address or password was incorrect. I was expecting something like 127.0.0.1 etc. but I don't know that ::1 is going to help as it would be nice to then see what country the ip address is coming from.
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 400 total points
ID: 41819889
To get IP's even if behind such a Proxy or other matters you can try using one of the following functions
function get_client_ip_env() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
 
    return $ipaddress;
}

Open in new window

or
function get_client_ip_server() {
    $ipaddress = '';
    if ($_SERVER['HTTP_CLIENT_IP'])
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if($_SERVER['HTTP_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
 
    return $ipaddress;
}

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:Black Sulfur
ID: 41819913
Your second example gives me undefined index errors and the first one still gives me ::1

Just to make sure I am using it correctly, I am just echoing the function name i.e.:

echo get_client_ip_env();

Open in new window

0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41819918
Not sure if it makes a difference, but I am using mamp on my local machine for this, not actually on a live server.
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 41819937
Yes you're using it correctly.
Anyway, as MAS already said, that's the IPV6 localhost IP.
I guess you're testing some script on your local system and if so, that's why you got that IP.
I don't believe you could check the country for 127.0.0.1. I guess you'd obtain a place somewhere in the world called Localhost ;-)
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41819951
Okay, so this will only work on a live server?
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 41819965
You could use a free dns service that points to your router and navigate to your local site using the dns given address.
For example I use free dyndns.org for this purpose
1

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question