A simple class to get the IP address of the visitor
Recently I needed to get the IP address of the visitor to an ASP.Net (C#) web application to log into the a hit log table in the db.
Though it is fairly easy; it has some tricky points.
Here is the class to get the IP of the client:
The code is self explanatory, I suppose.
calling AddressInfo.getIP(); anywhere in your asp.net web application will give the user's proper IP address.
Hope that it helps someone.
bu yaziyi sevdin mi?
hemen
una ekle!
Though it is fairly easy; it has some tricky points.
Here is the class to get the IP of the client:
using System.Web;
/*
* sarmal.com - 2006 (C)
* http://www.sarmal.com/
*/
namespace com.sarmal.net
{
public sealed class AddressInfo
{
public static string getIP(HttpRequest request)
{
string REMOTE_ADDR = request.ServerVariables["REMOTE_ADDR"];
/*
* if the request is redirected through
* a proxy, then the correct IP is this one.
*/
string HTTP_X_FORWARDED_FOR = request.
ServerVariables["HTTP_X_FORWARDED_FOR"];
if (HTTP_X_FORWARDED_FOR != null)
{
return HTTP_X_FORWARDED_FOR;
}
else
{
return REMOTE_ADDR;
}
}
private AddressInfo()
{
}
}
}
The code is self explanatory, I suppose.
calling AddressInfo.getIP(); anywhere in your asp.net web application will give the user's proper IP address.
Hope that it helps someone.
bu yaziyi sevdin mi?
hemen
una ekle!
- permalink: 2:43 PM


0 Coments
Post a Comment
Links to this post:
Create a Link
<< Home