-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
102 lines (92 loc) · 3.02 KB
/
index.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!doctype html>
<html>
<head>
<title>GeoLite2 Web Service Demo</title>
</head>
<body>
<?php
function h($input)
{
return htmlspecialchars($input, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
?>
<form method="POST">
<label>
IP Address to look up:
<input type="text" name="ip_address" value="<?= isset($_POST['ip_address']) ? h($_POST['ip_address']) : 'me' ?>">
</label>
<input type="submit" value="Perform lookup">
</form>
<?php
require_once 'vendor/autoload.php';
use GeoIp2\WebService\Client;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['ip_address']) && $_POST['ip_address']) {
// This creates a Client object that can be reused across requests. To
// use GeoIP2 instead of GeoLite2, you can remove the last two
// parameters in the client constructor, that is the arrays containing
// both the locale and the host.
$client = new Client(
getenv('MM_ACCOUNT_ID'),
getenv('MM_LICENSE_KEY'),
['en'],
['host' => 'geolite.info']
);
// You can replace "city" with the method corresponding to the web
// service that you are using, e.g., "country".
$record = $client->city($_POST['ip_address']);
?>
<table>
<tr>
<th>IP Address</th>
<td><?= h($record->traits->ipAddress) ?></td>
</tr>
<tr>
<th>Network</th>
<td><?= h($record->traits->network) ?></td>
</tr>
<tr>
<th>Country ISO code</th>
<td><?= h($record->country->isoCode) ?></td>
</tr>
<tr>
<th>Country name (en)</th>
<td><?= h($record->country->name) ?></td>
</tr>
<tr>
<th>Country name (zh-CN)</th>
<td><?= h($record->country->names['zh-CN']) ?></td>
</tr>
<tr>
<th>Most specific subdivision ISO Code</th>
<td><?= h($record->mostSpecificSubdivision->isoCode) ?></td>
</tr>
<tr>
<th>Most specific subdivision name</th>
<td><?= h($record->mostSpecificSubdivision->name) ?></td>
</tr>
<tr>
<th>City name</th>
<td><?= h($record->city->name) ?></td>
</tr>
<tr>
<th>Postal code</th>
<td><?= h($record->postal->code) ?></td>
</tr>
<tr>
<th>Latitude</th>
<td><?= h($record->location->latitude) ?></td>
</tr>
<tr>
<th>Longitude</th>
<td><?= h($record->location->longitude) ?></td>
</tr>
<tr>
<th>Accuracy Radius</th>
<td><?= h($record->location->accuracyRadius) ?>km</td>
</tr>
</table>
<?php
}
?>
</body>
</html>