Analyzing site access logs is crucial for any website. However, constantly checking logs through the server backend is tedious, and many VPS providers don’t even offer access log functionality. Here’s a handy tip for generating WordPress access logs with customizable content, saving you from having to log into the server panel every time.
Add the following code to your theme’s function.php file:
make_log_file();
function make_log_file() {
// Log filename
$filename = 'mylogs.txt';
// Exclude rc-ajax comments and cron mechanism access records
if (strstr($_SERVER["REQUEST_URI"], "rc-ajax") == false
&& strstr($_SERVER["REQUEST_URI"], "wp-cron.php") == false) {
$word = date('mdHis', $_SERVER['REQUEST_TIME'] + 3600 * 8) . " ";
// Visited page
$word .= $_SERVER["REQUEST_URI"] . " ";
// Protocol
$word .= $_SERVER['SERVER_PROTOCOL'] . " ";
// Method, POST or GET
$word .= $_SERVER['REQUEST_METHOD'] . " ";
//$word .= $_SERVER['HTTP_ACCEPT'] . " ";
// Browser info
$word .= getbrowser() . " ";
// Query parameters
$word .= "[" . $_SERVER['QUERY_STRING'] . "] ";
// Referrer URL
$word .= $_SERVER['HTTP_REFERER'] . " ";
// IP address
$word .= getIP() . " ";
$word .= "\n";
$fh = fopen($filename, "a");
fwrite($fh, $word);
fclose($fh);
}
}
// Get IP address (common code from the web)
function getIP() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} else if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} else if (getenv('REMOTE_ADDR')) {
$ip = getenv('REMOTE_ADDR');
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
// Get browser info (mobile and tablet data not yet included)
function getbrowser() {
$Agent = $_SERVER['HTTP_USER_AGENT'];
$browser = '';
$browserver = '';
if (ereg('Mozilla', $Agent) && ereg('Chrome', $Agent)) {
$temp = explode('(', $Agent);
$Part = $temp[2];
$temp = explode('/', $Part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[0];
$browser = 'Chrome';
}
if (ereg('Mozilla', $Agent) && ereg('Firefox', $Agent)) {
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode('/', $Part);
$browserver = $temp[2];
$temp = explode(' ', $browserver);
$browserver = $temp[0];
$browser = 'Firefox';
}
if (ereg('Mozilla', $Agent) && ereg('Opera', $Agent)) {
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode(')', $Part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[2];
$browser = 'Opera';
}
if (ereg('Mozilla', $Agent) && ereg('MSIE', $Agent)) {
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode(';', $Part);
$Part = $temp[1];
$temp = explode(' ', $Part);
$browserver = $temp[2];
$browser = 'Internet Explorer';
}
if ($browser != '') {
$browseinfo = $browser . ' ' . $browserver;
} else {
$browseinfo = $_SERVER['HTTP_USER_AGENT'];
}
return $browseinfo;
}
OK, a mylogs.txt file will be generated in your site’s root directory, accessible at https://imzl.com/mylogs.txt. The logs generated this way are far more precise than those from third-party analytics tools like CNZZ. You can see who accessed which files, which search engine bots crawled your site, and much more.