PHP External IP - php

I am using software called gns3 to build networks.
I have the need to find out devices external ip from within an gns3 internal network. This would be like a pc behind a router doing NAT.
Please could someone tell me if there is a way that you can echo out the clients external ip in php so I can add a web server to one of my virtual networks within gns3 so I can visit it on some devices and find their external ip’s for testing?
Thanks in advance

You need to make a request to an external website that could tell you the ip address you have. You may use checkip.dyndns.org - click the link to see your own ip addres.
if you want to do that from php here is an example code:
<?php
//get website content as a string
$ipCheckUrl = 'http://checkip.dyndns.org';
$subject = file_get_contents($ipCheckUrl);
//extract from string just ip address
$pattern = '/Current IP Address\:\s*(\d+\.\d+\.\d+\.\d+)/U';
$ip = preg_match($pattern,$subject,$result) ? $result[1] : 'Error';
//print ip address.
echo $ip;

Related

geoplugin php giving country and ip of hosting server

I'm using geoplugin php to get the country and ip of visitors on my site, but its returning the ip and country of hosting server instead of user who visit the site, I'm using Hostinger as my hosting.
$ipdat = #json_decode(file_get_contents("http://www.geoplugin.net/json.gp"));
$country = $ipdat->geoplugin_countryName;
$ip = $ipdat->geoplugin_request;
its working fine on xampp localhost.
Your code behavior is an expected behavior since the GEO plugin site gets the IP address from where the script request came and gives details accordingly. If you want user/visitor's data, then you will need to make use of user's remote IP stored in $_SERVER global variable and pass this IP to GEO plugin to get the details as mentioned in their docs.
Snippet:
<?php
$data = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$country = $data['geoplugin_countryName'];
$ip = $data['geoplugin_request'];
echo $country," ",$ip,"<br/>";
print_r($data);

How to get the Client IP that request a Tor WebPage?

i'm running an hidden service as the final exam of my school, but i've a problem: i have to show that the IP of the same computer which connects to my website can change in a bit , but when i try functions such as $ip=$_SERVER['REMOTE_ADDR']; it always gives me 127.0.0.1 instead of the last tor node which connects to me.
How can i solve this ? Is it normal on the Tor network? Cheers in advance!
There is a no direct way , but you can try using a third party server to get actual ip even from localhost server.
<?php
$content = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $content, $ip);
echo $externalIp = $ip[1];
?>
Here is a working PHPFiddle . Hope this will help, cheers :)

How to get the IP address of the user and return the data in JSON

I am not a php expert. I develop android apps. In my app i am getting the user's ip address from this url http://ip2country.sourceforge.net/ip2c.php?format=JSON. As you can see when some open this url it returns some info including the IP address. I only want to get the IP and (if possible) country. Most of the time this url is busy and doesn't returns ip and gives max active user connections error. Can you please give me any php file which i can put in my own webhost and call the url to get ip. The data returned should be in json so i can parse it easily.
Thanks
<?php
$json = file_get_contents("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
//this $json will have the response that the website sends.
echo json_encode($json);
?>
You can have this object wherever you call this php file and do the needful
Run this php file to check the output
Another way: EDIT
<?php
$visitor_ip = $_SERVER['REMOTE_ADDR'];
echo $visitor_ip;
$data1 = file_get_contents("http://api.hostip.info/?ip=$visitor_ip");
echo "<br> $data1";
?>
You can use PHP to get the users IP address via $_SERVER['REMOTE_ADDR']. You can then use an IP to Location lookup website to translate that into a country and merge the results:
$ip = $_SERVER['REMOTE_ADDR'];
$loc = json_decode(file_get_contents('http://ipinfo.io/'.$ip.'/json'), true);
$country = isset($loc['country']) ? $loc['country'] : 'Unknown';
$result = array('ip'=>$ip, 'country'=>$country);
header('Content-Type: application/json');
echo json_encode($result);
You get the IP address in PHP you have to use $_SERVER['REMOTE_ADDR'] then use http://ipinfo.io to pass that IP to this website. You can get the Location through JSON data, just follow the first answer on this question Getting the location from an IP address.
I have successfully implemented it by following the answer.

Get user ip address for geolock (not 192.168 address)

Im geo locking our program, we have a service that returns the country code for us. But if someone is behind a router, they have a local 192.168.x.x address, and this tells me nothing. Is there a way to find out their address at the router? I could do a trace, but then i need to program logic myself about which ip is useful and which are not. I'm doing this code fromt he server in php, and our client web app is in javascript. Please advise.
Thanks
function get_real_ip() {
if (!empty($_SERVER['HTTP_X_FORWARED_FOR']))
{
$client_ip = $_SERVER['HTTP_X_FORWARED_FOR'];
}
elseif (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$client_ip = $_SERVER['HTTP_CLIENT_IP'];
}
else
{
$client_ip = $_SERVER['REMOTE_ADDR'];
}
return $client_ip;
}
For server based software the PHP variable $_SERVER['REMOTE_ADDR'] will give the address of the client. For clients behind a router their address will be translated by the router firmware, so the server will see the router's public IP address, not the client's local private address.
All this is further confused by the possible presence of intervening proxy servers which may or may not tell you what their client address is. You can look at $_SERVER['HTTP_X_FORWRDED_FOR'] or $_SERVER['HTTP_CLIENT_IP'], if they're present. If not, you'll have to make do with the proxy address.

I am getting same IP for every system - How to get IP Address via PHP coding

Hello i am using this function to get IP Address of different systems..but everytime it returns the same value: 117.239.82.182
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
EDIT: (The answer changed radically after some clarifications in the comments)
You could edit the User-Agent setting of the user's browsers. To see how to change the setting in various browsers follow this link. Then you should modify your PHP script to read User-Agent of the browser.
In PHP,$_SERVER['HTTP_USER_AGENT'] returns the browser's User-Agent setting. Eg. you can define as User-Agent something like Company/System/1.02 Bla bla bla. Then when you receive that same string you can assume it is coming from a known host.
Attention that the User-Agent can be easily spoofed. So this method is not secure. The secure solution would be to implement a VPN solution.
117.239.82.182 is an external IP address. If all the systems that connect to the PHP server are behind the same external IP address, all of them will be notet as the same IP address.
Your script doesn't take the local IP. Don't think it's even possible. The IP you are seeing, is the IP of the firewall of your company.

Categories