Thinkphp comes with its own log system, and the generated files are under Runtime. Today we will introduce a natively written log insertion method to record the visitor's IP, access time and browser type.
Generate log class <?php <br />
/*<br>
* Log records<br>
* Generate a log file every day. When the file exceeds the specified size, back up the log file and regenerate a new log file<br>
*<br>
*/<br>
class Log {<br>
<br>
Private $maxsize = 1024000; //Maximum file size 1M<br>
<br>
//Write to log<br>
Public function writeLog($filename,$msg){<br>
$res = array();<br>
$res['msg'] = $msg;<br>
$res['logtime'] = date("Y-m-d H:i:s",time());<br>
<br>
//If the log file exceeds the specified size, back up the log file<br>
If(file_exists($filename) && (abs(filesize($filename)) > $this->maxsize)){<br>
$newfilename = dirname($filename).'/'.time().'-'.basename($filename);<br>
rename($filename, $newfilename);<br>
}<br>
<br>
//If it is a new log file, remove the first character comma in the content<br>
If(file_exists($filename) && abs(filesize($filename))>0){<br>
$content = ",".json_encode($res);<br>
}else{<br>
$content = json_encode($res);<br>
}<br>
<br>
//Append the log content after the log file content<br>
file_put_contents($filename, $content, FILE_APPEND);<br>
}<br>
<br>
<br>
//Read log<br>
Public function readLog($filename){<br>
If(file_exists($filename)){<br>
$content = file_get_contents($filename);<br>
$json = json_decode('['.$content.']',true);<br>
}else{<br>
$json = '{"msg":"The file does not exist."}';<br>
}<br>
Return $ json; <br>
}<br>
}<br>
?>
Write and read logs$Log = new Log();<br>
$Log->writeLog($filename, $msg);<br>
$loglist = $Log->readLog($filename);
View the generated log: http://www.sucaihuo.com/js/903.html
PHP records user access IP, browser type and access time to txt log file.zip ( 5.08 KB Download: 0 times )