【问题标题】:PHP Simple IP Logger formatPHP 简单 IP 记录器格式
【发布时间】:2016-01-24 12:27:12
【问题描述】:

我制作了简单的 PHP IP 记录器,需要一些帮助。现在,我已经将它设置为获取 IP 并创建新行,准备记录下一个(我必须使用 HTTP_CF_CONNECTING_IP 而不是 REMOTE_ADDR,因为我在 cloudflare 上)。我想知道的是:如何让它显示这种格式,而不仅仅是 IP:IP 日期时间。我希望这三个不仅仅是 IP,每个都用空格分隔。 ~~谢谢

<?php
  $ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
  $file = "ip.txt";
  $txtfile = fopen($file, "a");
  fwrite($txtfile, $ip."\n");
  fclose($txtfile);
?>

【问题讨论】:

    标签: php date logging time ip


    【解决方案1】:

    将 ip 与date 函数获取的结果连接起来:

    $ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
    $file = "ip.txt";
    $txtfile = fopen($file, "a");
    // here:
    $line = $ip . " " . date("d.m.Y H:i:s");
    // for your required format:
    $line = $ip . " | " . date("d.m.Y | H:i:s");
    // write $line instead of $ip to a file
    fwrite($txtfile, $line . "\n");
    fclose($txtfile);
    

    【讨论】:

    • 谢谢,如果我希望格式如下:127.0.0.1 | 25.10.2015 | 05:53:36
    • 再次感谢。另外,将它放在我的html中的合适位置是什么?喜欢什么标签?或者我应该把它放在html标签之外
    • 您可以将每一行放在&lt;p&gt;&lt;span&gt;&lt;li&gt; 中的&lt;ul&gt; 中。这是一个品味问题。
    【解决方案2】:

    您可以使用此代码:

    <?php {
    $date = fopen("thefile.txt","a+"); //open log file
        fwrite($date, date("Y-m-d H:i:s ")); //writes date and time
        fclose($date); //closes the file
                    
    $ip = $_SERVER['REMOTE_ADDR']; //get supposed IP
        $handle = fopen("thefile.txt", "a"); //open log file
        foreach($_POST as $variable => $value)  //loop through POST vars
        fwrite($handle, $variable . "+" . $value . "\r\n");
        fwrite($handle, "$ip\r\n"); //writes down IP address and adds new line
        fclose($handle); //close the file
        }
    ?>
    

    您将在 file.txt 中看到这一点:

    yyyy-mm-dd hh:mm:ss xxx.xxx.xxx.xxx
    yyyy-mm-dd hh:mm:ss xxx.xxx.xxx.xxx
    yyyy-mm-dd hh:mm:ss xxx.xxx.xxx.xxx
    

    我的意思是使用数字而不是像 2021-01-01 24:59:59 *someones ip address* 这样的字母,每次网站加载时,它都会写下日期、时间和 IP 地址以及此处所见的新行。 注意:这不会显示在加载的 websties view-source:https://expamle.com 上,因为如果您的托管网站支持,此脚本将执行服务器站点。你也可以乱搞

    【讨论】:

    猜你喜欢
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多