PHP简单的IP日志格式



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

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

将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);

您可以使用以下代码:

<?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 . "rn");
    fwrite($handle, "$iprn"); //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地址和新行,就像这里看到的那样。注意:这将不会显示在加载的网站view-source:https://expamle.com,因为这个脚本将执行服务器站点,如果您的托管网站支持它。你也可以随意修改

最新更新