Click to See Complete Forum and Search --> : Retrieve IP address from User
evaleah
July 21st, 2003, 02:27 PM
I am trying to retrieve an IP address for my users when they commit certain transactions. The code I was using before I updateded to Framework 1.1 and VS 2003 was:
Dim sIP As String
Dim oAddr As Object
With GetHostByName(GetHostName())
oAddr = New System.Net.IPAddress(.AddressList(0).Address)
sIP = oAddr.ToString
End With
Now I am getting an error that says the .AddressList(0).Address piece is obselete.
I also tried Request.UserHostAdress but this returned the OS address and not the broadcasted one.
I looked at the documentation on this and for some reason cannot make heads or tails of what should replace the address list. It looks to me like it is still appropriate. Anyone have any ideas about how this should be now?
Thanks!
Satishpp
July 21st, 2003, 05:04 PM
The REMOTE_ADDR and REMOTE_HOST server variables will give you the users IP.
Satish
evaleah
July 22nd, 2003, 03:55 PM
I know I am probably just being thick, but could you give me an example bit of code of how to use those?
Thanks!
anupam kant
July 23rd, 2003, 01:30 AM
simply by:
Request("REMOTE_HOST") or Request("REMOTE_ADDR")
coolbiz
July 23rd, 2003, 07:34 AM
Whose IP address are you trying to log? The web server or the client?
-Cool Bizs
evaleah
July 24th, 2003, 09:49 AM
The client.
coolbiz
July 24th, 2003, 01:21 PM
Page.Request.UserHostName or Page.Request.UserHostAddress
-Cool Bizs
anupam kant
July 25th, 2003, 01:10 AM
I think
Page.Request.UserHostAddress and
Request("REMOTE_ADDR")
give the same result, i.e. client machine's IP.
Isn't it??
coolbiz
July 25th, 2003, 06:54 AM
It is but Request("REMOTE_ADDR") is there for the backward compatibility purpose. It is recommended to use the .NET built-in properties and routines (counterparts) instead.
:) Cool Bizs
anupam kant
July 25th, 2003, 07:22 AM
Thanks...
evaleah
July 25th, 2003, 12:39 PM
Thanks for the suggestion CoolBiz. However, I have already tried that and it is not returning the correct value. Please see the quote below from my original message.
Originally posted by evaleah I also tried Request.UserHostAdress but this returned the OS address and not the broadcasted one.
There may, of course, be some other piece I am missing. If so, please let me know. But setting sIP = Page.Request.UserHostAddress returned 127.0.0.1 and not the broadcasted IP address of the user.
Thanks again!
coolbiz
July 25th, 2003, 01:15 PM
The only way you can get 127.0.0.1 (this is also known as the loopback IP or localhost IP) from the client is if you're browsing the page on the same machine where your webserver is.
If that is the case, and you actually want to get the LAN IP, make sure you do not use http://localhost/....../somepage.aspx but instead http://hostname/..../somepage.aspx.
-Cool Bizs
evaleah
July 28th, 2003, 12:17 PM
Thank you Cool Bizs. Can we say 'DOH'.
Of course I was testing it on the same PC as I was hosting the site. What was I thinking?
When I accessed the page via the hostname it worked. Thanks for the heads up.
Eva
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.