是否可以将访问者的IP和位置写入文件


Is it possible to write the IP and location of the visitor in a file?

是否可以将访问者的IP和位置写入文件?这是我的代码:

<?php $line = date('Y-m-d H:i:s') . " - $_SERVER[REMOTE_ADDR]"; file_put_contents('visitors.log', $line . PHP_EOL, FILE_APPEND); ?>

但它只是把IP写下来。

现在我有这个Javascript的代码(但这只是GET位置和ip,是的,我知道我是菜鸟):

<script> $.get("http://ipinfo.io", function (response) {
$("#ip").html("IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region);
$("#details").html(JSON.stringify(response, null, 4)); }, "jsonp");</script>

是否可以将其放入文件中?提前谢谢你。

您必须在

ajax 的帮助下将收到的数据发送到服务器。然后在服务器上,设置 PHP 脚本来接收数据,然后将其存储为文件。出于安全目的,你不能直接用javascript来做(javascript不允许文件编辑)。

.JS:

$.ajax({
    type:"GET",
    url:"script.php",
    data : { ip: myIP, location : myLocation },
.....
});

.PHP:

<?php
$ip = $_GET['ip'];
$location = $_GET['location'];
// File handling...
?>