How to get remote ip Address in magento? - php

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

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.

$_SERVER['REMOTE_ADDR'] is not returning ip address

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.

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;

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