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

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?)

Related

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.

Get the ip address of client in php

I am using the following function for getting the ip address of client.
function get_client_ip() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else
if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
But I check the results it gives me IP address different than my machine IP address on server.(I think it's the IP of my organization server) Any solution I can get the real IP address of my client.
Edit for Possible duplicate: I have read the answer of possible duplicate question.
My problem is that It's not giving me the ip address of my machine.
Edit-2 Live link
http://stashad.com/nodegates/voter.php?incr=blockvotes
try this snippet.,
if your computer is localhost or connected on network its shows their original IP
function getIP($ip = null, $deep_detect = TRUE){
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
$ip = $_SERVER["REMOTE_ADDR"];
if ($deep_detect) {
if (filter_var(#$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if (filter_var(#$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
return $ip;
}
usage
echo getIP();
if deep_detect set to be TRUE check client IP is forwarded or not
echo getIP(null,true);
Your computer is not directly connecting to the server; it is connecting via an intermediary device--something like a router, or a gateway in a large company.
The server can generally only get the IP address of the connecting device. In some cases things like load balancers will add headers in the request that include an originating IP address (e.g. HTTP_X_FORWARDED_FOR) but I don't think these will generally include the private IP address of the machine that made the request.
If you look at the diagram below, you are one of the computers but the router makes the actual connection to the server, rather than your computer itself.

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.

Get IP address of user using 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.

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