In PHP, I want to save messages to a text file and read the log file using php.
Here is what I have so far:
function LogUserActivity($LogFile, $Activity){
$UserIp = $_SERVER['REMOTE_ADDR'];
$TimeRef = date('d-m-Y H:i T');
$Handle = fopen($LogFile, 'a');
$Data = $UserIp.'|'.$TimeRef.'|'.$Activity.'~';
fwrite($Handle, $Data);
fclose($Handle);
}
function ReadUserActivity($LogFile){
global $log;
$LogFile = file_get_contents($LogFile);
$ExplodedLogFile = explode("~", $LogFile);
$ArrayNum = count($ExplodedLogFile);
$i = 0;
while ( $i <= $ArrayNum ){
$log[$i] = explode("|", $ExplodedLogFile[$i]);
$i++;
}
}
Call
LogUserActivityat the beginning of every script, passing it the file you want to write to and some message to yourself. E.g.:Call the other function in the script you’re gonna use to inspect the logs. It will set the $log variable which you can then print out however you want.
Or just use this single liner: