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.
Related
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.
I did find some info about saving IP and I came up with the code below:
// IP GRAB
$http_client_ip = $_SERVER['HTTP_CLIENT_IP']; //MORE RELIABLE IP
$http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR']; //ALSO MORE RELIABLE
$remote_addr = $_SERVER['REMOTE_ADDR']; //NOT RELIABLE BCS IT MAY BE A SHARED NETWORK OR BEHIND A PROXY
if (!empty($http_client_ip)) {
$ip_address = $http_client_ip;
} else if (!empty($http_x_forwarded_for)) {
$ip_address = $http_x_forwarded_for;
} else {
$ip_address = $remote_addr;
}
$ip = ip2long($ip_address);
I'd like to grab the user's IP address when the user push to submit button/sent form. I also want to save this IP to the table on the database. So I made an IP column on the table as INT(11) UNSIGNED(This was recommended for somebody else on here.)
In short: Am I doing it right?
Your code can extended, the follow function could be used to get client IP:
function get_client_ip() {
$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;
}
It is also not necessary to change your the IP to long (ip2long), only if you have a reason to do it.
And I will save it as String (VARCHAR) in database.
This is what I came up after some more search:
I changed the INT(11)UNSIGNED to VARCHAR(45) and I just save IP without using ip2long(); the rest is the same as it was before. Now it adds ::1 to the IP column on the table when I post a test. I assume it works alright and I'll check it out when I actually publish the site.
note: I'll accept this answer when it becomes available. Thank you for your time.
I am using $_SERVER['REMOTE_ADDR'] in php to find client's ip address.
$ipaddress=$_SERVER['REMOTE_ADDR'];
echo $ipaddress;
which return ::1
I also tried the following code,but that gives me same result as well.
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';
What am I doing wrong?How can I get clients ip?
I would use the ip to find client's location via ipinfo.io.
Thanks for your time.
::1 is the actual IP. It is an ipv6 address (i.e. localhost). If you were using ipv4 it would be 127.0.0.1.
If you want to get a different IP address, then you'll need to connect to the server through a different network interface.
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!!
i used following code to display remote ip address
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
and the following code also
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
elseif(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
elseif(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();
but both code display this result ::1
plzzzzz help me friends! how to get remote IP address?
Try this
//Get Visitor's information
$visitorData = Mage::getSingleton('core/session')->getVisitorData();
// printing visitor information data
echo "<pre>"; print_r($visitorData); echo "</pre>";
// user's ip address (visitor's ip address)
$remoteAddr = Mage::helper('core/http')->getRemoteAddr(true);
// server's ip address (where the current script is)
$serverAddr = Mage::helper('core/http')->getServerAddr(true);
In Magento, you have to get the remove/customer IP address in the following way
Print Mage::helper('core/http')->getRemoteAddr(true);
It's the IPV6 version of localhost.
Switch to ipv4 (if neccesary) by changing your apache listen port (in httpd.conf) to Listen 0.0.0.0:80 and restart apache. This forces the webserver to use the ipv4 ip. $_SERVER['REMOTE_ADDR'] will now return 127.0.0.1.
See a discussion about this here: http://board.issociate.de/thread/489575/SERVERquotREMOTEADDRquot-returning-1.html