Created
October 27, 2011 19:18
-
-
Save meeDamian/1320553 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* LOGGING FUNCTION | |
** | |
* $str => string to be logged | |
* $type => type of logging: | |
* - access: all non-error ACTIONS generated by users | |
* - user : same as 'access' | |
* - error : all possible ERRORS generated by users | |
* - admin : all important access/error actions generated by ADMIN | |
** | |
* returns given string | |
**/ | |
function toLog($str, $type="access", $fid=-1) { // log given string of given type (optional: for given user) | |
// TODO: add error handling !! | |
$log_path = dirname(__FILE__); | |
switch( $type ) { | |
case 'error': | |
$fname = 'err.log'; break; | |
default: | |
case 'access': | |
case 'user': | |
$fname = 'users.log'; break; | |
case 'admin': | |
$fname = 'admin.log'; break; | |
} | |
$log_str = date('[Y-m-d H:i:s]') . " " . preg_replace('/\s+/', ' ', str_replace("\n", "", $str))." ip => " . getIP() . ", user-agent => " . $_SERVER['HTTP_USER_AGENT'] . "\n"; | |
// save to general log files | |
$f = fopen($log_path . "/log/" . $fname, "a+"); | |
fwrite($f, $log_str); | |
fclose($f); | |
// save to user log file | |
if($fid != -1 and $fid != "" and ctype_digit($fid)) { | |
$user_log_path = $log_path . '/users-data/' . $fid; | |
if(!file_exists($user_log_path)) mkdir($user_log_path); | |
$g = fopen($user_log_path . '/log_'.$fid.'.log', "a+"); | |
fwrite($g, $log_str); | |
fclose($g); | |
} | |
// return logged string | |
return $str; | |
} | |
/** | |
* GETS CURRENT USER IP | |
** | |
* returns ip of current user | |
**/ | |
function getIP() { // get IP of current connection; called from toLog | |
$ip = (empty($_SERVER['HTTP_CLIENT_IP']) ? | |
(empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? | |
$_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']) : $_SERVER['HTTP_CLIENT_IP']); | |
return str_replace("unknown, ", "", $ip); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment