|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
A simple event logger written in PHP.
The event logger (event_logger.php)
|
<?PHP
/*
PHP Event Logger By Ethan Sterling
Usage: 'bool' log_event('string' $file, 'string' $info, 'string', $script)
Purpose: Resize an image
Parameters:-
file: Which file should the event be logged to
info: Any information that you wish to be stored in the log
Such as information about any error that may have occoured
script: Which script triggered the log event, defaults to the contents
of $_SERVER['PHP_SELF']. If the log event is being called from
within a file that has been included, set this value to __FILE__
else use $_SERVER['PHP_SELF'] or ignore this parameter
Returned values:-
Returns TRUE on success
Example:-
log_event("log.txt", $_SERVER['HTTP_USER_AGENT']);
This example will log the clients user-agent, as sent by the browser, to the
file 'log.txt', and will use the default value for $script ($_SERVER['PHP_SELF'])
*/
function log_event($file, $info, $script = "")
{
//Get the IP of the computer that triggered the log event
$ip = $_SERVER['REMOTE_ADDR'];
//Get the time that the log event was triggered
$time = time();
//Work out what script called the log event
//This is needed because variables cannot be used as default values in a function
if($script == "")
{
$script = $_SERVER['PHP_SELF'];
}
//Build the log string containing logged IP, time, script and event information
$string = "$ip - $time - $script - $info\r\n";
//Open the log file for writing (or create and open it if it does not exist)
if(!$f = fopen($file, "a")) return FALSE;
//Write to the log file
if(!fwrite($f, $string)) return FALSE;
//Close the logfile
fclose($f);
//Return a value of TRUE to indicate that the function has finished successfully
return TRUE;
}
?> | |
The example script, that calls the logger
| <?PHP
//Include the event logger script
include('event_logger.php');
//Log the users user agent (as reported bu the browser) to the file log.txt
log_event("log.txt", $_SERVER['HTTP_USER_AGENT']);
?> | | |
|
| Making a simple Hit-Log using PHP and MySql Categories : PHP, Log Files, Beginner Guides, Databases, MySQL | | | DB Connection Function with error handling and email failure notices Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging | | | Current Page's URL using PHP Categories : PHP, Beginner Guides, Global Variables | | | Get the self URL of current page Categories : PHP, URLs, Beginner Guides | | | Newbie Notes #1 - Making a form return to itself Categories : PHP, Beginner Guides, HTML and PHP | | | Automatic global variable definer Categories : PHP, Variables, Beginner Guides | | | Multiple Select box, Select multiple Items from Menu.List box Categories : PHP, HTML and PHP, Beginner Guides | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | A very simple PHP single password cookie based login without usernames. Categories : PHP, Cookies, Security, Beginner Guides | | | Newbie Notes #3 - What went wrong? A useful little debugging aid Categories : PHP, Beginner Guides, Debugging | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Logwriter Categories : PHP, Log Files | | | A beginner's session handling class Categories : PHP, PHP Classes, Sessions, Beginner Guides | | | logger class (PHP5 +) Categories : PHP, PHP Classes, Log Files, XML | | | Form Submission Using Array's Categories : PHP, HTML and PHP, Beginner Guides, Arrays | |
|
|