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
  1. require_once 'clientipaddress.php';
  2. require_once 'validateipaddress.php';
  3.  
  4. function write_log($logfile, $textline=false) {
  5.     global $log_dir;
  6.  
  7.     $ipaddress = client_ip_address();
  8.  
  9.     if (!validate_ip_address($ipaddress)) {
  10.         return false;
  11.     }
  12.  
  13.     $timestamp=date('Y-m-d H:i:s');
  14.  
  15.     $logmsg="$timestamp $ipaddress";
  16.     if ($textline) {
  17.         $logmsg .= "\t$textline";
  18.     }
  19.     $logmsg.="\n";
  20.  
  21.     $file = isset($log_dir) ? ($log_dir . DIRECTORY_SEPARATOR . $logfile) : $logfile;
  22.  
  23.     $r = @file_put_contents($file, $logmsg, FILE_APPEND);
  24.  
  25.     return $r;
  26. }
SEE ALSO

track, clientipaddress, validateipaddress

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].