Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

get users IP address showing ::1

Posted on 2016-09-28
8
Medium Priority
?
183 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 28

Assisted Solution

by:MAS
MAS earned 400 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 23

Accepted Solution

by:
Ferruccio Accalai earned 1600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 23

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 23

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

876 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