geoplugin php giving country and ip of hosting server - php

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);

Related

PHP External IP

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;

Redirection of IP to Minecraft server with PHP

I have a local Minecraft Server set up. I decided to get myself my own domain, because ips arent really pretty. The problem is, that because the ip address of my router changes every night, I can't just do a A-record to my ip address. Instead I need a dynamic dns provider which allows me the use of my own domain. I could not seem to find one, so I coded it by myself with php (I have a free web server with a static ip address). Here's the code of the .php-file:
<?
$usernameTest = $_GET["username"];
$passTest = $_GET["pass"];
$ipaddr = $_GET["ipaddr"];
$username = "USERNAME";
$pass = "*****";
$port = ":25565";
$serverIPtxt = "serverIP.txt";
if(file_exists($serverIPtxt)) {
if($usernameTest == $username) {
if($passTest == $pass) {
$a = fopen("$serverIPtxt", "w");
fwrite($a, $ipaddr);
fclose($a);
echo $ipaddr;
}
} else {
$a = fopen("$serverIPtxt", "r+");
$dynIP = fread($a, filesize($serverIPtxt));
fclose($a);
$url="http://".$dynIP."".$port;
header("Location: $url", true);
die();
}
}
?>
My router is automaticly applying the correct ip address, so in theory I should be able to connect to the minecraft server with my new domain, but I cant. Instead Minecraft gives me this error:
[13:52:38] [Client thread/INFO]: Connecting to DOMAIN, 25565
[13:52:39] [Server Connector #5/ERROR]: Couldn't connect to server
java.net.ConnectException: Connection refused: no further information: DOMAIN/IPADDRESS:25565
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[?:1.8.0_25]
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:716) ~[?:1.8.0_25]
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:208) ~[NioSocketChannel.class:4.0.23.Final]
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:287) ~[AbstractNioChannel$AbstractNioUnsafe.class:4.0.23.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:528) ~[NioEventLoop.class:4.0.23.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) ~[NioEventLoop.class:4.0.23.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) ~[NioEventLoop.class:4.0.23.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) ~[NioEventLoop.class:4.0.23.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) ~[SingleThreadEventExecutor$2.class:4.0.23.Final]
at java.lang.Thread.run(Thread.java:745) ~[?:1.8.0_25]
What am I doing wrong? Or does Minecraft just not support php redirects?
Minecraft does not use HTTP! It uses its own protocol based on TCP.
The best option, which I have used in the past, is to run a dynamic ip updater client.
Get yourself a No-IP domain name (e.g. myname.ddns.net)
Download the dynamic updater client (available for Windows, Mac or Linux)
Set your custom domain name as a CNAME to point to myname.ddns.net (your NoIP domain name)
Give players your custom domain name (e.g. myname.com). This will refer the client to myname.ddns.net through the CNAME record which will in turn refer to your dynamic IP (e.g. xxx.xxx.xxx.xxx) as an A record.
After this you will be able to connect to your server with your custom domain and the dynamic updater will keep the dynamic IP up to date automatically.

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 :)

Extract Host Name from "gethostbyaddr" string

I wish to extract the exact hostname from an ip address but what i am receiving is an entire string of the address.
I would just like to extract only the Host Name from the the string. Can any one point me in the right direction ?
Here is the snippet of the code being used:-
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$fullhost = gethostbyname(gethostbyaddr($ip));
$host = preg_replace("/^[^.]+./", "*.", $fullhost);
?>
Host: <?=$host?>
The output received from it is :-
Host: *.36.64.182.airtelbroadband.in
I would just like to display Airtel Broadband and nothing else to the user
You can try this:
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']); // gets the full hostname
$hostNames = explode(".", $host); // explodes into parts divided by DOT
echo $hostNames[count($hostNames)-2]; // take what you need
// $hostNames[count($hostNames)-1]; -> in
// $hostNames[count($hostNames)-2]; -> airtelbroadband
You might also want to look into geoip.
This is a basic PHP extension that can be installed using PECL.
string geoip_isp_by_name ( string $hostname )
The geoip_isp_by_name() function will return the name of the Internet Service Provider (ISP) that an IP is assigned to.
This function is currently only available to users who have bought a commercial GeoIP ISP Edition. A warning will be issued if the proper database cannot be located.
Consider this simple example:
<?php
$subject = 'Host: *.36.64.182.airtelbroadband.in';
$pattern = '/^Host:\s+((\*|\d+)\.){4}(.+)\..+$/';
preg_match($pattern, $subject, $tokens);
var_dump($tokens[3]);
The output obviously is:
string(15) "airtelbroadband"
That is all the information you can rip from your input. If you really want to somehow show the "publicly known company names" (as opposed to the technical network names), then you need to use some form of catalog. It is very hard to somehow retrieve such information directly from the network. You could give the whois database a try. But parsing the output of that will be a really tough task...

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.

Categories