a visitor log on 1 of sites lost visitors. it's been working year , half entries lost overnight. log page here (if visit 1 of site pages, information added log): http://mykindred.com/dalton/hoax/viewlog.php
the log kept in text file ($filename) should limit $maxloglines = 300. $logline contains new visitor added log. code generates log:
$lines = file($filename, file_ignore_new_lines | file_skip_empty_lines); $logline .= implode("\n", array_slice($lines, 0, $maxloglines)); file_put_contents($filename, $logline); any suggestions why log lose entries? have coding error?
when writing files using file_put_contents, using series of fopen(), fwrite() , fclose() file opened file mode w:
open writing only; place file pointer @ beginning of file , truncate file 0 length. if file not exist, attempt create it.
as not have exclusive lock on file, possible read file @ point @ file has been truncated , file pointer placed @ start. in case, contents of file() command empty.
instead, should use option lock_ex secure exclusive lock of file while writing:
file_put_contents($filename, $logline, lock_ex); you can read more @ in flock() documentation.
Comments
Post a Comment