What I'm trying to achieve here is to get local ip address of computer that is connecting to machine with public IP address.
We have server with public IP address (let's say X). Also we have 15 computers in our office with local ip addresses (192.168.20....). I want to know which computer changed something on our system. We are all going out on the internet with public IP Y. As we all know with $_SERVER['REMOTE_ADDR']
I can only get address Y, but can't get local IP address (192.168.20.N).
So I've installed webserver on one of the local machines and then I'm sending ajax (jsonp
) request to that local machine, I get local IP address of computer and than send that IP address to public server in every request. everything works fine and I'm getting both local and public IP address. But I wonder if there is any better way of doing this?
here is my test.js script
$(document).ready(function(){
var _ip = '';
$.ajax({
url: 'http://192.168.20.24/getIP.php', //local machine
dataType: 'jsonp',
success: function(response){
_ip = response;
$.ajax({
url: 'getIP.php', //public address
type: 'POST',
dataType: 'json',
data: {
ip: _ip
},
dataType: 'json',
success: function(response){
$('#local').html(response.local);
$('#public').html(response.public);
}
})
}
});
});
local getIP.php
header('content-type: application/json; charset=utf-8');
echo $_GET['callback'] . '('.json_encode($_SERVER['REMOTE_ADDR']).')';
and public getIP.php
echo json_encode(array('local' => $_POST['ip'], 'public' => $_SERVER['REMOTE_ADDR']));
Thanks in advance
Note: code is written for testing purposes only.