note 74548 added to function.fwrite

From: Date: Tue, 17 Apr 2007 01:16:22 +0000
Subject: note 74548 added to function.fwrite
Groups: php.notes 
Request: Send a blank email to php-notes+get-125486@lists.php.net to get a copy of this message
Here is a simple class which can be used to write data to a log file. It captures the date/time of the log entry, the user's remote IP address, and a definable $string. Each entry is appended as a new line in the log file. A new log file is created daily named YYYYMMDD.log. <?php /* usage: define('LOG_PATH', '/path/to/save/log/files'); $log = new logger(); $log->write('message string to write in log file'); */ class logger { var $ip = null; var $file = null; // --- function: constructor --- // function logger() { $this->ip = $_SERVER['REMOTE_ADDR']; $this->file = LOG_PATH.'/'.date('Ymd').'.log'; } // --- function: write --- // function write($string) { if($fp = fopen($this->file, 'a')) { if(flock($fp, LOCK_EX)) { // do exclusive lock $log_entry = date('Y-m-d H:i:s')." | ".$this->ip." | ".$string."\r\n"; fwrite($fp, $log_entry); flock($fp, LOCK_UN); // release the lock fclose($fp); } } } } // end class ?> ---- Server IP: 203.16.234.20 Probable Submitter: 203.31.198.126 ---- Manual Page -- http://www.php.net/manual/en/function.fwrite.php Edit -- https://master.php.net/note/edit/74548 Del: integrated -- https://master.php.net/note/delete/74548/integrated Del: useless -- https://master.php.net/note/delete/74548/useless Del: bad code -- https://master.php.net/note/delete/74548/bad+code Del: spam -- https://master.php.net/note/delete/74548/spam Del: non-english -- https://master.php.net/note/delete/74548/non-english Del: in docs -- https://master.php.net/note/delete/74548/in+docs Del: other reasons-- https://master.php.net/note/delete/74548 Reject -- https://master.php.net/note/reject/74548 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125486) next »