Solved

get users IP address showing ::1

Posted on 2016-09-28
8
75 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
  • 4
  • 3
8 Comments
 
LVL 25

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
 

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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
 

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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending an image to a php webservice via cURL from command line 7 29
comma true 6 32
Cookie not unsetting 7 18
Cpanel file manager 8 22
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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

805 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