-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlookup-ip.php
38 lines (30 loc) · 895 Bytes
/
lookup-ip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
require_once 'functions.php';
require_once 'session.php';
rtgui_session_start();
$ip = $_REQUEST['ip'];
if (!ip2long($ip)) {
die(json_encode(array(
'ip' => $ip,
'error' => 'Invalid IP'
)));
}
if (is_array($_SESSION["ip-$ip"])) {
die(json_encode($_SESSION["ip-$ip"]));
}
// What hostip.info returns happens to look a lot like HTTP headers
$url = "http://api.hostip.info/get_html.php?ip=$ip";
$info = parse_http_response(file_get_contents($url));
$info = $info[0];
$info['hostname'] = gethostbyaddr($ip);
$info['country_short'] =
preg_replace('@^[^(]+\((.*)\)[^)]*$@', '\1', $info['country']);
foreach (array('city', 'country', 'country_short') as $key) {
if (stristr($info[$key], 'unknown') !== false) {
$info[$key] = 'unknown';
}
}
$info['location'] = $info['city'] . ', ' . $info['country_short'];
$_SESSION["ip-$ip"] = $info;
print json_encode($info);
?>