Getting accurate IP through proxy - php

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!!

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.

When load website via iframe, which IP address will be recognized in the website?

So there are 2 websites A and B.
A has a page with iframe which loads B.
And B has the back-end code to detect visitor's IP address like following:
// Function to get the user IP address
function getUserIP() {
$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_X_CLUSTER_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
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;
}
If a user visits the iframe page in A, which IP address will be fetched?
Will it be client's IP address,
or
site A 's IP address?
An iframe element represents a nested browsing context. The iframe source is requested by the user browser. So if you check for IP address you will get the client IP.
I guess it will still get the client's IP address?
Because iframe will send the almost same header I guess.
The client IP address will be sent, too.

How to get server's public IP address using PHP?

I need to get Public IP address of the system using PHP. I tried the following function:
function get_server_ip() {
$ipaddress = '';
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');
return $ipaddress;
}
But, I got only the localhost IP 127.0.0.1. I am in need of the server's public IP address. What function is available in PHP to get my public IP address which I get from this site: http://www.whatismyip.com/
It is not possible to get Your IP address on that way if you run web server on same machine or local LAN where you run your browser client, you will always get local IP address.
If You still want to get Your public address from PHP script, on *nix servers which runs in same network as your browser client, you can get it via command like this:
$myPublicIP = trim(shell_exec("dig +short myip.opendns.com #resolver1.opendns.com"));
echo "My public IP: ".$myPublicIP;

Categories