我有这个ip logger php脚本,它运行良好,但我需要在每个日志中添加国家名称..我该怎么做?
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$dateTime = date('Y/m/d G:i:s');
$file = "visitors.html";
$file = fopen($file, "a");
$data = "<pre><b>User IP</b>: $ip <b> Browser</b>: $browser <br>on Time : $dateTime <br></pre>";
fwrite($file, $data);
fclose($file);
echo "<h1>Hi :)</h1>";
?>
答案 0 :(得分:0)
<?php
$ip = "8.8.8.8";
$ip_info = json_decode(file_get_contents("http://ip-api.com/json/$ip"), true);
echo "<pre>";
echo print_r($ip_info, true);
echo "</pre>";
?>
你应该得到输出:
Array
(
[as] => AS15169 Google LLC
[city] => Mountain View
[country] => United States
[countryCode] => US
[isp] => Google
[lat] => 37.4229
[lon] => -122.085
[org] => Google
[query] => 8.8.8.8
[region] => CA
[regionName] => California
[status] => success
[timezone] => America/Los_Angeles
[zip] =>
)
这里有你的代码,它将是这样的:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$ip_info = json_decode(file_get_contents("http://ip-api.com/json/$ip"), true);
$country = $ip_info['country'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$dateTime = date('Y/m/d G:i:s');
$file = "visitors.html";
$file = fopen($file, "a");
$data = "<pre><b>User IP</b>: $ip <b> Browser</b>: $browser <br>on Time : $dateTime <br>from Country : $country</pre>";
fwrite($file, $data);
fclose($file);
echo "<h1>Hi :)</h1>";
?>