Solved

get users IP address showing ::1

Posted on 2016-09-28
8
62 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 24

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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now