好的,基本上脚本可以正常工作,但每次刷新data.txt文件都会被覆盖,我希望它将每个单独的条目添加到新文件中的data.txt中。
示例:1.1.1.1 7月18日@ 2:03:17
<?php
date_default_timezone_set("Europe/London");
function GetAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$adresseip = GetAddr();
function sec2hms($sec, $padHours = false) {
@$hms = "";
@$days = intval($sec/86400);
if($days > 0 ) {
if($days == 1) {
@$hms .= (($padHours)?str_pad($hours, 2, "0", STR_PAD_LEFT).':':@$days.' Day');
} else {
@$hms .= (($padHours)?str_pad($hours, 2, "0", STR_PAD_LEFT).':':@$days.' Days');
}
}
@$sec-= ($days*86400);
@$hours = intval(intval($sec) / 3600);
if($hours > 0) {
if($days > 0) { @$s = ', '; }
if($hours == 1) {
@$hms .= @$s.(($padHours)?str_pad($hours, 2, "0", STR_PAD_LEFT).':':@$hours.' Hour');
} else {
@$hms .= @$s.(($padHours)?str_pad($hours, 2, "0", STR_PAD_LEFT).':':@$hours.' Hours');
}
}
@$minutes = intval(($sec / 60) % 60);
if($minutes > 0) {
if($hours > 0) { @$d = ', '; }
if($minutes == 1) {
@$hms .= @$d.str_pad($minutes, 2, "0", STR_PAD_LEFT) . ' Minute';
} else {
@$hms .= @$d.str_pad($minutes, 2, "0", STR_PAD_LEFT) . ' Minutes';
}
}
@$seconds = intval($sec % 60);
if($seconds > 0) {
if($minutes > 0) { @$p = ', '; }
if($seconds == 1) {
@$hms .= @$p.str_pad($seconds, 2, "0", STR_PAD_LEFT) . ' Second';
} else {
@$hms .= @$p.str_pad($seconds, 2, "0", STR_PAD_LEFT) . ' Seconds';
}
}
return @$hms;
}
function report($data) {
$time = date('g:i:s A', time());
echo "[$time] $data\n";
}
$d= date('F jS @ g:i:s a');
$fp = fopen('data.txt', 'r+');
fwrite($fp,"$adresseip - $d");
fclose($fp);
?>
答案 0 :(得分:0)
这:
$fp = fopen('data.txt', 'r+');
将指针放在文件的开头,导致覆盖。使用
$fp = fopen('data.txt', 'a+');
代替。
文档中的更多内容:http://php.net/manual/en/function.fopen.php:
'r +'开放阅读和写作;将文件指针放在文件的开头。
'a +'开放阅读和写作;将文件指针放在文件的末尾。如果该文件不存在,请尝试创建它。
答案 1 :(得分:0)
You need to use fopen
in append mode:
$fp = fopen('data.txt', 'a+');
当您使用fopen
模式r+
时,文件指针位于文件的开头,这会导致您覆盖数据。