Jump to content

Simple IP and HOST loging with $log->save()


EyeDentify
 Share

Recommended Posts

Hello Fellow PW fans.

Tought i share a simple but usefull way of utilizing the $log system in PW to do simple loging on what IP visit the site and also the HOST.

Its not fancy, and its very basic, see it as a boiler plate. You can get all kinds of fancy with it.

Remember that the $log system automaticly records the URL from where it was called so we don´t need to supply that to our Entry.

I just wanted a simple entry with IP and HOST, the date is also recorded with the entry automaticly.

The beauty is that i can watch the logs from the ADMIN page almost in realtime.


I am using the "markup regions" output method so i put the code in my _main.php file.
You could just put it wherever like to be included with every page load in your site.

I hardcoded my logfile to be named ip_log_site but you can use what you feel is better.

Also made sure the loging do not happen if your logged in as admin (Super user).

See this PW Doc for more info:
https://processwire.com/api/ref/wire-log/save/
 

<?PHP
function logIP($session, $log) {
            
    /* get IP info */
    $ip_adress = $session->getIP();
    $host_address = gethostbyaddr($ip_adress);
        
    /*
    	log IP adress info
        NOTE: 'ip_log_site' is a hardcoded name 
        of the logfile, change for your preference.
    */
    $log->save("ip_log_site", "IP: {$ip_adress}, Host: {$host_address}");
        
}
/* if Super User (Admin) do not log */
if(!$user->isSuperuser()) {
    logIP($session, $log);
}
?>

Hope anyone have use for it.

 

/EyeDentify

  • Like 2
Link to comment
Share on other sites

Thanks for sharing this, EyeDentify – nice and simple ?

Just a little note before folks go implementing IP logging: IP addresses are considered personal data, and as such you should keep GDPR (and similar legislations) in mind. Here's SE question that covers the details related to logging IP addresses: https://law.stackexchange.com/questions/28603/how-to-satisfy-gdprs-consent-requirement-for-ip-logging. (The general consensus seems to be that you don't need to ask for permission before logging IP addresses, but there are other requirements you do need to keep in mind.)

  • Like 5
Link to comment
Share on other sites

12 hours ago, teppo said:

Thanks for sharing this, EyeDentify – nice and simple ?

Just a little note before folks go implementing IP logging: IP addresses are considered personal data, and as such you should keep GDPR (and similar legislations) in mind. Here's SE question that covers the details related to logging IP addresses: https://law.stackexchange.com/questions/28603/how-to-satisfy-gdprs-consent-requirement-for-ip-logging. (The general consensus seems to be that you don't need to ask for permission before logging IP addresses, but there are other requirements you do need to keep in mind.)

Your Welcome.

It´s kind of my thing to try to keep things simple and understandable.
Not just for myself but also so other could have use for my code.

I will think about the GDPR issue.

Have a nice day.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...