Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IP Address Logging for Lots of Models in Existing Application

I have an existing app that has many, many models. I'd like to log the IP address of the user that created them with the primary purpose being a way to help weed out spammers or other abusive users (if I don't know what IP address(es) they are using, I can't block it). I would need to keep track of these over time as users may access from home, the office, their phone, etc. and I'd want to see usage patterns. It also might be kind of fun to map out where users are visiting from or something, but any side-effects are purely thought-stuff at this time.

We use the cookie-based method of storing user sessions.

I can think of two ways of doing this (I'll create them as replies so people can vote):

  1. Add IP address attribute to every model, and pass that in
  2. Some sort of Logger model that is called with an observer or after_save callback

Thoughts? Are there better ways? Plugins that do this? Thanks!!

like image 798
Matt Rogish Avatar asked Nov 25 '25 06:11

Matt Rogish


1 Answers

I would use a polymorphic association for this, and then you can apply it to all your models that need to be tracked. That keeps the messyness of adding an IP address column to every model to a minimum and will dramatically cut down on the amount of duplication across your codebase. Then going the observer route is probably the easiest way to hook up to your models.

like image 69
Scott Miller Avatar answered Nov 26 '25 19:11

Scott Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!