Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Linc's avatar
Level 1

Save visitor ip in Database.

Hi guys!

I need to save the ip adress of my guests when they visit one of my specific page

Is that possobile?

Like a table for save the ip address, one for my different page.

In my model for pages:

public function userActivities()
    {
        return $this->hasMany('App\_user_actrivity');
    }

In my model for userActivity:

public function page()
    {
        return $this->belongsTo('App\Page');
    }
0 likes
5 replies
Linc's avatar
Level 1

Thsank you @tray2 , i resolved with another trick:

$user_activity = new Visitor;
        $user_activity->ip_address=$request->ip();
        $user_activity->house_id= $house->id;
        $user_activity->save();
martinbean's avatar

@linc Be sure you’re legally covered to do so. For example, in the EU this would be considered “personally identifiable information” and you’d need a genuine, legal basis to record the IP address of a user.

Also, try and stick to Laravel’s conventions for naming. You model should be called App\UserActivity and not “_user_actrivity” [sic]. I’m not sure why you have a preceding slash at all to be honest, and you have a typo in “activity”.

Linc's avatar
Level 1

@martinbean Yep; i tried this only in my localserver.

And yes, "_user_actrivity" in my error.

Thank you!

artcore's avatar

I don't know why you want to track your visitors' IPs but I tried for a custom analytics solution on sites with 1000s of daily hits. Conclusion:You can't track visitors by IP - highly unreliable. Use cookies (with consent).

Why? Mobile ISPs assign IPs round robin out of their pool every time you (re)connect to the network. And that's 60-70% of the traffic.

1 like

Please or to participate in this conversation.