Skip to content

Instantly share code, notes, and snippets.

@zuzmeister
Forked from bencevans/gist:3513313
Last active November 23, 2019 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zuzmeister/e9301fc9fb23a425fb042b8269c9d6a6 to your computer and use it in GitHub Desktop.
Save zuzmeister/e9301fc9fb23a425fb042b8269c9d6a6 to your computer and use it in GitHub Desktop.
php: create visitor log file
<?php
class log {
public $filename;
public $timestamp;
public $ip;
public $u_agent;
public $u_refer;
public $ub;
public $uos;
public $log;
public $stream;
public $username;
// Find out the Username the visitor used to log in (as stored in .htpasswd)...
// ...and save ist to member-variable $username
private function setUserName(){
$this->username = '';
// Try to get the login name from the $_SERVER variable.
if (isset($_SERVER['HTTP_AUTHORIZATION']) || isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
$authorization_header = '';
if (isset($_SERVER['HTTP_AUTHORIZATION']) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
$authorization_header = $_SERVER['HTTP_AUTHORIZATION'];
}
// If using CGI on Apache with mod_rewrite, the forwarded HTTP header appears in the redirected HTTP headers.
elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && !empty($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
$authorization_header = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
}
// Resemble PHP_AUTH_USER and PHP_AUTH_PW for a Basic authentication from
// the HTTP_AUTHORIZATION header. See http://www.php.net/manual/features.http-auth.php
if (!empty($authorization_header)) {
list($username_temp, $userpass_temp) = explode(':', base64_decode(substr($authorization_header, 6)));
$this->username = $username_temp;
}
}
// Check other possible values in different keys of the $_SERVER superglobal
elseif (isset($_SERVER['REDIRECT_REMOTE_USER'])) {
$this->username = $_SERVER['REDIRECT_REMOTE_USER'];
}
elseif (isset($_SERVER['REMOTE_USER'])) {
$this->username = $_SERVER['REMOTE_USER'];
}
elseif (isset($_SERVER['REDIRECT_PHP_AUTH_USER'])) {
$this->username = $_SERVER['REDIRECT_PHP_AUTH_USER'];
}
elseif (isset($_SERVER['PHP_AUTH_USER'])) {
$this->username = $_SERVER['PHP_AUTH_USER'];
}
return $this->username;
}
public function logger($filename) {
$this->filename = $filename;
$this->timestamp = date('l d F Y H:i:s');
$this->ip = $_SERVER['REMOTE_ADDR'];
$this->u_agent = $_SERVER['HTTP_USER_AGENT'];
$this->req_uri = $_SERVER['REQUEST_URI'];
$this->ub = '';
$this->uos = '';
$this->setUserName();
if(!isset($_SERVER['HTTP_REFERER'])){
$this->u_refer = 'No referrer';
}
if (preg_match('/MSIE/i', $this->u_agent)):
$this->ub = "Internet Explorer";
elseif (preg_match('/Firefox/i', $this->u_agent)) :
$this->ub = "Mozilla Firefox";
elseif (preg_match('/Chrome/i', $this->u_agent)) :
$this->ub = "Google Chrome";
elseif (preg_match('/Flock/i', $this->u_agent)) :
$this->ub = "Flock";
elseif (preg_match('/Opera/i', $this->u_agent)) :
$this->ub = "Opera";
elseif (preg_match('/Netscape/i', $this->u_agent)) :
$this->ub = "Netscape";
elseif (preg_match('/Safari/i', $this->u_agent)) :
$this->ub = "Apple Safari";
endif;
if (preg_match('/iphone/i', $this->u_agent)):
$this->uos = "iphone";
elseif (preg_match('/Android/i', $this->u_agent)) :
$this->uos = "Android";
elseif (preg_match('/Linux/i', $this->u_agent)) :
$this->uos = "Linux";
elseif (preg_match('/Mac OS x/i', $this->u_agent)) :
$this->uos = "Mac OS x";
elseif (preg_match('/Mac OS x 10.4/i', $this->u_agent)) :
$this->uos = "Mac OS x 10.4 - Tiger";
elseif (preg_match('/Mac OS x 10_5_5/i', $this->u_agent)) :
$this->uos = "Mac OS x 10.5 - Leopard";
elseif (preg_match('/Mac OS x 10.5/i', $this->u_agent)) :
$this->uos = "Mac OS x 10.5 - Leopard";
elseif (preg_match('/Mac OS x 10_6_2/i', $this->u_agent)) :
$this->uos = "Mac OS x 10.6 - Snow Leopard";
elseif (preg_match('/Mac OS x 10.6/i', $this->u_agent)) :
$this->uos = "Mac OS x 10.6 - Snow Leopard";
elseif (preg_match('/Mac OS x 10_7_2/i', $this->u_agent)) :
$this->uos = "Mac OS x 10.7 - Lion";
elseif (preg_match('/Mac OS x 10.7/i', $this->u_agent)) :
$this->uos = "Mac OS x 10.7 - Lion";
elseif (preg_match('/Windows 3.1/i', $this->u_agent)) :
$this->uos = "Windows 3.1";
elseif (preg_match('/Windows 95/i', $this->u_agent)) :
$this->uos = "Windows 95";
elseif (preg_match('/Win95/i', $this->u_agent)) :
$this->uos = "Windows 95";
elseif (preg_match('/Windows 98/i', $this->u_agent)) :
$this->uos = "Windows 98";
elseif (preg_match('/Windows NT 5.0/i', $this->u_agent)) :
$this->uos = "Windows 2000";
elseif (preg_match('/Windows 2000/i', $this->u_agent)) :
$this->uos = "Windows 2000";
elseif (preg_match('/Windows NT 5.1/i', $this->u_agent)) :
$this->uos = "Windows XP";
elseif (preg_match('/Windows NT 5.2/i', $this->u_agent)) :
$this->uos = "Windows Server 2003";
elseif (preg_match('/Windows NT 6.0/i', $this->u_agent)) :
$this->uos = "Windows Vista";
elseif (preg_match('/Windows NT 6.1/i', $this->u_agent)) :
$this->uos = "Windows 7";
elseif (preg_match('/Windows NT 6.2/i', $this->u_agent)) :
$this->uos = "Windows 8";
elseif (preg_match('/Windows Phone OS 7/i', $this->u_agent)) :
$this->uos = "Windows Phone 7";
elseif (preg_match('/Windows Mobile/i', $this->u_agent)) :
$this->uos = "Windows Mobile";
elseif (preg_match('/Windows CE/i', $this->u_agent)) :
$this->uos = "Windows CE";
endif;
$this->log = array($this->timestamp, $this->ip, $this->username, $this->u_refer, $this->ub, $this->uos, $this->req_uri, $_SERVER['HTTP_USER_AGENT']);
$this->stream = fopen($filename, "a");
fputcsv($this->stream, $this->log);
fclose($this->stream);
}
}
$logged = new log();
$logger = $logged->logger("log.csv");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment