Active Server Pages Programming (ASP)/IP Address
Expert: Jeff Allen - 6/10/2006
QuestionDear Sir,
I need to show visitor's IP address upon my site. I know VB.Net up to intermediate level. Kindly guide me in detail. I shall be very thankful to you.
Regards,
Adeel
AnswerThere's got to be a number of ways of doing this... Although I am no VB.NET guru by any means. One solution I found is:
There are two server variables of interest; REMOTE_ADDR and HTTP_X_FORWARDED_FOR. As many visitors access the internet via a third party (ie their ISP), REMOTE_ADDR does not always contain their IP address... it contains their ISP's address. If this is the case, most browsers then store the users IP address in the HTTP_X_FORWARDED_FOR variable. So, first, we check HTTP_X_FORWARDED_FOR, and then if that is empty, we try REMOTE_ADDR instead:
Dim sIPAddress
sIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If sIPAddress="" Then sIPAddress = Request.ServerVariables("REMOTE_ADDR")
One other thing worth bearing in mind. If the user also accesses the internet via a Proxy server, then HTTP_X_FORWARDED_FOR will contain the Proxy's IP address. If this is the case, there is no way to get the users 'real' IP address.
Otherwise this article on 15Seconds (
http://www.15seconds.com/issue/021119.htm) seems to explain it quite well also.