52
log
write_log
SYNOPSIS
write_log($logfile, $textline=false)
DESCRIPTION
write_log appends a line to the log file $logfile in the folder $log_dir.
Each line begins with the current date and time and the client IP address followed by $textline separated by a tab character.
The global variable $log_dir is defined in config.inc.
Check the write access rights on this folder.
write_log returns the number of bytes written to the log file, or false if the client IP address is invalid or the log file cannot be written.
EXAMPLE
php > require 'library/log.php';
php > global $log_dir;
php > $log_dir = 'log';
php > write_log('enter.err', 'admin');
$ cat log/enter.err
2026-08-13 17:02:47 127.0.0.1 admin
CODE
- require_once 'clientipaddress.php';
- require_once 'validateipaddress.php';
- function write_log($logfile, $textline=false) {
- global $log_dir;
- $ipaddress = client_ip_address();
- if (!validate_ip_address($ipaddress)) {
- return false;
- }
- $timestamp=date('Y-m-d H:i:s');
- $logmsg="$timestamp $ipaddress";
- if ($textline) {
- $logmsg .= "\t$textline";
- }
- $logmsg.="\n";
- $file = isset($log_dir) ? ($log_dir . DIRECTORY_SEPARATOR . $logfile) : $logfile;
- $r = @file_put_contents($file, $logmsg, FILE_APPEND);
- return $r;
- }
Comments