How can you keep track of which terminals a user is currently connected from?

One of the great things about Quora is that when you log out, it can tell you what other credentials (i.e. your session) are still valid. It allows you to see when this session will start and record this session.

How can Quora do this? How can this function be repeated?

I do most of my coding in ruby ​​on rails, so if anyone wants to answer in this context, that would be great, but I'm open to looking at the answers regardless.

+3


source to share


1 answer


When you make an HTTP request to the server, the headers sent along with the request include the IP address from which the request originated. The complete set of headers is available in your controller, and the IP address is available with request.client_ip

.

To get the hostname associated with this IP address, you need to do a "reverse DNS lookup" like this:



require "socket"

Socket.gethostbyname(request.client_ip)

      

It probably requires a small library in app/lib

which you can require

in application_controller.rb

. It's just a few lines of code, but requires a good name :-)

0


source







All Articles