timestamp - PHP fwrite over-riding whole file -
okay script works, every time refresh data.txt file over-written, want add each individual entry data.txt in new file.
example: 1.1.1.1 july 18th @ 2:03:17 pm
<?php date_default_timezone_set("europe/london"); function getaddr() { if (!empty($_server['http_client_ip'])) //check ip share internet { $ip=$_server['http_client_ip']; } elseif (!empty($_server['http_x_forwarded_for'])) //to check ip pass 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); ?>
this:
$fp = fopen('data.txt', 'r+'); puts pointer @ beginning of file, causing overwrite. use
$fp = fopen('data.txt', 'a+'); instead.
more in documentation: http://php.net/manual/en/function.fopen.php:
'r+' open reading , writing; place file pointer @ beginning of file.
'a+' open reading , writing; place file pointer @ end of file. if file not exist, attempt create it.
Comments
Post a Comment