I was wondering how to get the current users online and their IP address.
I know how to get a clients IP adress and I know how to use cookies and sessions for current users.
What i need is the logic to get the IP of the current user, and if the user leaves the site, it will stop tracking the users and stop getting the users IP.
Is that possible?
Have a look at
print_r($_SERVER)– there’s loads of info in there about the current user.If the user leaves the site, you can’t detect that immediately – but you can keep a track of the last time you saw a user in your database, and if you don’t see a user for (say) 20 minutes, invalidate their cookie.
Edit: one thing you could do is have a periodic AJAX operation that says ‘this user is still here’ every 30 seconds or so. However this arguably puts more stress on your web server than is necessary – the earlier option I outline is probably better.