[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

getting client ip address

Stewart

2/12/2007 8:29:00 AM

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

--
Posted via http://www.ruby-....

3 Answers

Clifford Heath

2/13/2007 10:20:00 AM

0

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?

John O'gara

2/26/2008 4:10:00 PM

0

Clifford Heath 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


--
Posted via http://www.ruby-....

James Kasemi

2/26/2008 11:42:00 PM

0

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.ht...
--
Posted via http://www.ruby-....