Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get the ip address of the visitors of my website. I know it can be done using

string ipaddress;
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];

But i d't know where i want to put it.In which page master page or Default.aspx page.And how can i store this details in the database.And i want to check it how many of them are visiting our site ....Anyone can give acomplete picture of this.
Posted

http://forums.asp.net/t/1397341.aspx[^]

Check this link..hope it will help..
 
Share this answer
 
If your site uses Session for each user (i.e, login), then you can place the persistance logic in
C#
Global.asax/Session_start 
method.

If you are not using it, then you can create a base class and have the logic there.
So for pages where you want this functionality inherit from base page instead of inheriting from System.Web.UI.Page.
 
Share this answer
 
Comments
Member 9492907 16-Aug-13 4:07am    
In Global.asax/session_start we can place the string ipaddress;
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];


and where we are storing the ipaddress in the database
Suppose you want to get details of users who visited the contact page in your website. Then,in Page_Load event of the contact page just put the single line as below:
C#
protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
     {
           string ip = Server.HtmlEncode(Request.UserHostAddress);
     }
}

This will give you ip as a string. You can insert it into database wherever you want.

Note:

You will get 127.0.0.1 every time if you run it from localhost.

Regards.:)
 
Share this answer
 
v3
Comments
Member 9492907 16-Aug-13 4:59am    
if i am loading that page again and again the date will store again right? ...i d't wat it to be happend....for a particular user it should be same
Thanks7872 16-Aug-13 5:01am    
Refer to updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900