to me honest, I am not very experienced coder. But I would like have some simple bots logger for Wordpress. All what I need is just to log 'user-agent' name into some basic txt file.
Having date/time is just a bonus.
Does anyone knows some simple php code / snippet which I can put into header.php or functions.php please?
Thank you. Best wishes,
Milan
UPDATE: So solution by Kristopher Ives works perfectly, thank you.
And if I want put a time and date of the user-agent visit what I should inset into:
add_action('wp', function(){
file_put_contents(__DIR__.'/useragents.log', $_SERVER['HTTP_USER_AGENT']."
", FILE_APPEND);
});
How it should look like? I do something wrong with characters , . ; I am confused :(
You can use simple function, to write something to file:
function simpleLogger ($logMsg){
file_put_contents('./log_'.date("j.n.Y").'.log', $logMsg, FILE_APPEND);
}
In your Wordpress plugin you'll need to hook into every page request and write to a file:
add_action('wp', function(){
file_put_contents(__DIR__.'/useragents.log', $_SERVER['HTTP_USER_AGENT']."
", FILE_APPEND);
});
However, be aware that many PHP environments don't have write access to files for security reasons, so you will need to chmod g+w
that file so it's writable by PHP. Also be aware that this information likely already exists in /var/log/apache/*access.log
as well as many existing Wordpress plugins that will save this information to a database and provide basic reporting functionality for you, such as WP Statistics
I figured it out :)
add_action('wp',function () {
$timestamp = date('d/m/Y h:i:s ');
file_put_contents(__DIR__ .'/useragents.log', $timestamp.$_SERVER['HTTP_USER_AGENT']."
",FILE_APPEND); }
);