Click here to Skip to main content
15,915,094 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I want to catch the system ip address when user login in my website

That means

when user login in my website i have to catch user system ip address

please help me
Posted

Try this function:

C#
public static string getclientIP()
{
    string result= string.Empty;
    string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ip))
    {
        string[] ipRange = ip.Split(',');
        int le = ipRange.Length - 1;
        result = ipRange[0];
    }
    else
    {
        result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    return result;
}


Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard
 
Share this answer
 
Comments
Member 7932936 5-Dec-11 3:02am    
it shows the output as 127.0.0.1 but my ip address is not 127.0.0.1
[no name] 5-Dec-11 3:15am    
127.0.0.1 is localhost. this means you are working on your PC. If you try other PC to use your app, you will get a different ip address.
Member 7932936 5-Dec-11 3:55am    
i used on another pc also but i got same 127.0.0.1


I want client ip address
[no name] 5-Dec-11 4:03am    
what i mean is host your application in your IIS or on your server and another pc must use your application. with that, you will get a different IP. 127.0.0.1 is localhost, meaning, current IP address. Cheers :)
Member 7932936 5-Dec-11 4:50am    
thanks it's working fine
Try this
C#
protected void ip_addr()
    {
        string sIPAddress = null;
        sIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(sIPAddress))
            sIPAddress =Request.ServerVariables["REMOTE_ADDR"];

        SqlConnection con = new SqlConnection(conn);
        try
        {
            con.Open();
            string str = "insert into IPADDR_LOGIN(ipaddr,date_time,uname) values('" + Convert.ToString(sIPAddress) + "','" + Convert.ToString(DateTime.Now) + "','" + txt_usr.Text.ToString().Trim() + "')";
            SqlCommand cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
        }
        finally
        {
            con.Close();
        }
    }
 
Share this answer
 
Comments
Shahbaz435 17-Jun-15 3:38am    
the code not showing the ip address
C#
public string getipaddress()
    {
        string ipaddress;

        ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (ipaddress == "" || ipaddress == null)

            ipaddress = Request.ServerVariables["REMOTE_ADDR"];

        return ipaddress;
    }
 
Share this answer
 
C#
string ip;
        ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (!string.IsNullOrEmpty(ip))
        {
            string[] ipRange = ip.Split(',');
            string trueIP = ipRange[0].Trim();
        }
        else
        {
            ip = Request.ServerVariables["REMOTE_ADDR"];
        }
 
Share this 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