Get IP address of user using PHP - php

I know may be this questions asked here 1000 times, but i need a specific answer of my question here, I know my code is right but there is something else happening here which i don't know, so don,t duplicate my question please. Thanks :)
I am using a code to get the IP address.
Here is my code:
<?php
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR'&quot;);
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';
echo $ipaddress; // final ip will be here
?>
and the output is :
::1
I don't know why this giving me this number and not real IP, please help soon. Remember i am working on LOCALHOST

This output is the address of the server. It is an IPv6 address (equivalent of 127.0.0.1 in IPv4, which means localhost).
You are getting this because you are probably running this in localhost.

Related

How to get the device public ip address in PHP [duplicate]

This question already has answers here:
How to get the client IP address in PHP
(32 answers)
Closed last month.
I want to get the public ip of a user for socket connection in java. So that i need the user device public ip address for that.
i have made a weservice for that . which is get_ip.
But it returning the ip adress of my network, not my device's ip. because while i'm calling this api from my pc with connected my router and while i'm calling this api from my mobile it gives the same ip address.
the code is
header('Content-type: application/json');
$error->code = -200;
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$error->ip7 = getenv('HTTP_CLIENT_IP');
if(getenv('HTTP_X_FORWARDED_FOR'))
$error->ip8 = getenv('HTTP_X_FORWARDED_FOR');
if(getenv('HTTP_X_FORWARDED'))
$error->ip9 = getenv('HTTP_X_FORWARDED');
if(getenv('HTTP_FORWARDED_FOR'))
$error->ip10 = getenv('HTTP_FORWARDED_FOR');
if(getenv('HTTP_FORWARDED'))
$error->ip11 = getenv('HTTP_FORWARDED');
if(getenv('REMOTE_ADDR'))
$error->ip12 = getenv('REMOTE_ADDR');
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$error->ip1 = $_SERVER['HTTP_CLIENT_IP'];
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$error->ip2 = $_SERVER['HTTP_X_FORWARDED_FOR'];
if(isset($_SERVER['HTTP_X_FORWARDED']))
$error->ip3 = $_SERVER['HTTP_X_FORWARDED'];
if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$error->ip4 = $_SERVER['HTTP_FORWARDED_FOR'];
if(isset($_SERVER['HTTP_FORWARDED']))
$error->ip5 = $_SERVER['HTTP_FORWARDED'];
if(isset($_SERVER['REMOTE_ADDR']))
$error->ip6 = $_SERVER['REMOTE_ADDR'];
$json = json_encode($error);
echo $json;
return;
I have read some post over statcoverflow like this any many more, But got the same response.
please help me.
Try in addition
$_SERVER['REMOTE_ADDR'] use
$_SERVER['HTTP_X_REAL_IP'], should work, as a last resort, you can output the entire $_SERVER variable in the logs to check the IP change on devices, etc. (I correctly understood that it does NOT require an address in the user's local network?)

I'm getting the same ip address from different computers in same network

I'm trying to get the ip address of the client so that they only use one account per device.
However, when I use the following code, I found that the same ip address returned from the computers in the same network.
function get_client_ip() {
$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;
}
How can I get the correct ip address from different devices?
Most likely those computers are going from network through the router with NAT enabled.
In that case you will see only ip address of that router, nothing you can do with it.
But you can check combination of ip address and user-agent header:
$_SERVER['HTTP_USER_AGENT']
Unfortunately, there is no garaunteed way to 100% check the origin of request, but combination of ip address and user-agent could work for simple scenario.

returning invalid ip adres with $server or $env

im dealing with a problem ...
When i want to send the client's ip adress along with a form, and the ip adress that the form sends is this:
"2a02:a456:4012:X:dcXX:56f5:XXX:555a" And it keeps changing too...
Instead of an actual ip adress that i can use to as example ban people with, example given: "85.333.222.111".
I use this code:
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 = 'ONBEKEND';
return $ipaddress;
}
and i use this code:
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 = 'ONBEKEND';
return $ipaddress;
}
both returning these ip adresses:
"2a02:a456:4012:X:dcXX:56f5:XXX:555a
Also, very important: my .htaccess won't allow me when I deny all but my IP4 address. When I allow my IP6 address it allows me but that address changes from time to time.
Can somebody please point me in the right direction and tell me what im doing wrong here? Help would be really appreciated.
Kinds regards,
Julian.
I don't have the rep to comment, but check out this question/answer: PHP $_SERVER['REMOTE_ADDR'] shows IPv6

What's the most reliable method of obtaining the user's IP address?

I've been using $_SERVER["REMOTE_ADDR"] to obtain the user's IP address for months. Lately, I have noticed that this value may sometimes contain a proxy server IP and not the user's IP, which makes it of little use to me. (I have noticed this issue after I updated to PHP 7.1.0, although I've tried downgrading to the previous PHP version and the results were identical).
I have read tons of SO questions and most of them only address this problem without a solution, or offer the following function as a solution:
function get_ip() {
$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;
}
This is unreliable too, because these different variables can be spoofed.
Are there any good and reliable solutions to obtain the correct user IP address and not that of any intermediary proxy servers?
Use this function below;
function checkIPAddress()
{
// Get IP Address using $_SERVER['REMOTE_ADDR'];
$ipaddress = ($_SERVER('REMOTE_ADDR')) ? $_SERVER('REMOTE_ADDR') : '';
if ( filter_var ($ipaddress, FILTER_VALIDATE_IP) == false)
{
//Log bad IP attempt
}
else{
//Received valid IP Address, run next code here.
}
}
Allow HTTP_X_FORWARDED is a bad habit. Use it when you are doing proxy server, load balancing or when necessary etc.

Getting accurate IP through proxy

Currently our domain will point to certain proxy IP (due to protection service) and all the traffics will go through the proxy IP and only route back the valid traffic to our server.
Here is my problem, I have an application to capture visitor's IP and save to database once they had login to our member site, I have below function to get IP and its was saved as a test.php for testing purpose:
function get_client_ip() {
$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;
}
echo get_client_ip();
However if I access directly https://domain.com/test.php, the IP is not what is expected (it's totally different from what I checked in whatismyip.com). I've been contact to our service provider, they told we must access URL like https://x.x.x.x/~sitename/test.php (where x.x.x.x is our VPS server IP) to capture accurate visitor IP, YES! it's works when I tried the URL, but back to the technical issue in coding, how can I integrate https://x.x.x.x/~sitename/test.phpin this function to tell the code get IP from the provided URL??
Please help!!

Categories