javascript - 如何从单独的日志文件检索数据

标签 javascript php html server

我希望从位于我的服务器目录中的日志文件中检索内容,并将其放入我的索引页面上的 javascript 变量中(也在同一文件目录中)。 (在我的管理页面上,您可以使用一个运行 php 代码的表单来将 lat 和 lng 上传到日志文件),我真的需要一些帮助,因为我对此相当陌生!

以下 html 页面的文件位置是 (public_html/index.html)

    <!doctype=html>
    <html>
    <head>
        <title> Example </title>    
    </head>

    <body>
            //I DON'T KNOW IF THIS FORM IS NECESSARY
            <form action="retrieve-from-log.php" method="post">
            <input type="submit" value="Reload 
            the map" />
            </form>
            //BUT I WANT THE VALUE TO UPDATE ON PAGE LOAD OR SOMETHING


        <script>
            var coordinates = THIS IS WHERE I AM LOST;
            //script for initialising the google map
            function initMap(){

                var inställningar = {
                 zoom:15,
                 center:{THIS IS WHERE I WOULD NEED "coordinates" TO BE},
                }
                //new map
            var map = new google.maps.Map(document.getElementById('map'), 
            settings);

            //new marker pin
            var marker = new google.maps.Marker({
             position:{HERE ASWELL},
             map:map,               
            });
        }
    <script>
</body> 
</html>

我想要访问的日志文件位于(public_html/mylog.log)

lat:60.6754, lng:17.14174

这是我希望index.html中的变量的格式

php 文件位于 (public_html/retrieve-from-log.php)

<?php
$log_file_name = 'mylog.log'; // Change to the log file name
file_get_contents($log_file_name);
$lat = $_GET['lat']; // incoming message
$lng = $_GET['lng']; // incoming message

header('Location:/index.html'); // redirect back to the main site
?>

最佳答案

您写道,您的“日志文件”只会包含一行,如下所示:

纬度:60.6754,经度:17.14174

使用 PHP,您可以通过以下方式读取此类文件的内容:

<?php

$filename = 'mylog.log';
$content  = file_get_contents($filename);

参见:file_get_contents()在手册中。请注意该函数如何返回存储在变量中的文件内容。

现在我认为您可能希望将纬度和经度放在单独的 PHP 变量中。这样做的方法是:

<?php

$content     = "lat:60.6754, lng:17.14174";
$coordinates = explode(", ", $content);
$latitude    = substr($coordinates[0], 4);
$longitude   = substr($coordinates[1], 4);

现在您可以在 PHP 中使用这些 PHP 变量,例如放入 Javascript:

<script>
   var latitude  = <?php echo $latitude; ?>;
   var longitude = <?php echo $longitude; ?>;
   .... now you can use them in javascript ....
</script>

把它们放在一起给出:

<?php

$filename    = 'mylog.log';
$content     = file_get_contents($filename);
$coordinates = explode(", ", $content);
$latitude    = substr($coordinates[0], 4);
$longitude   = substr($coordinates[1], 4);

?>
<script>
   var latitude  = <?php echo $latitude; ?>;
   var longitude = <?php echo $longitude; ?>;
   .... now you can use them in javascript ....
</script>

您可以将其放入 index.php PHP 脚本中。

关于javascript - 如何从单独的日志文件检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58191815/

相关文章:

javascript - jQuery。如果找不到则替换链接

php - MySQL:查询设计问题

javascript - 如何将输入元素值设置为某个带有双引号的字符串?

internet-explorer - 模拟 Internet Explorer JavaScript 环境的 JavaScript 库?

javascript - Strapi - 获取上次内容更改日期

javascript - 拖拽轮播时如何调用函数?

php - 如何将 include() 函数放入 PHP 类中

用于搜索 MySQL 数据库的 PHP 脚本

html - 如何避免表格 <td> 单元格中出现文字换行?

javascript - 错误类型错误 : Cannot set property 'paginator' of undefined