Getting client ip address

whats the best way to get a client ip address. I am using rails if that
makes any difference

If you are using rails then this should get you going.

ip_addr = request.env[‘REMOTE_ADDR’]

cheers

Stewart wrote:

whats the best way to get a client ip address. I am using rails if that
makes any difference

It does - you need the socket, and if you’re in a Mongrel hiding behind
an Apache or IIS you’re out of luck. Otherwise, BasicSocket::getpeeraddr
is what you want - then you have to parse the sockaddr structure in a
platform-dependent way. Blech - why can’t Ruby do basic sockets right?

Clifford H. wrote:

Stewart wrote:

whats the best way to get a client ip address. I am using rails if that
makes any difference

It does - you need the socket, and if you’re in a Mongrel hiding behind
an Apache or IIS you’re out of luck. Otherwise, BasicSocket::getpeeraddr
is what you want - then you have to parse the sockaddr structure in a
platform-dependent way. Blech - why can’t Ruby do basic sockets right?

If you’re please behind a mongrel cluster you can use

request.env[‘HTTP_X_FORWARDED_FOR’].split(", ")

the first element in the resultant array should be the originating
client IP address

Stewart wrote:

whats the best way to get a client ip address. I am using rails if that
makes any difference

Use the remote_ip method of the request object. It looks for forwarded
requests automatically:

http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html#M000408