doushi5024 2014-11-22 14:52
浏览 50

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

is it possible to write the IP and Location of the visitor in a file? this is my code:

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

But its only writing the IP down.

Now i have this code for Javascript (but this is only GET location and ip and yes i know im noob):

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

Is it possible to put that in a file? Thank you in advanced.

  • 写回答

1条回答 默认 最新

  • duanhan9334 2014-11-22 14:58
    关注

    You've to send the data you received with the help of ajax to server. Then at server, set up PHP script to receive data and then store it as a file. You can't do it directly with javascript because of security purposes (javascript doesn't allow file editing).

    JS:

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

    PHP:

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

报告相同问题?