How to detect the user IP?
-
I need to know the user IP to give the right limit to access my website, how to detect the user IP? and moreover, how to know which country is the user from? thanks!
-
I need to know the user IP to give the right limit to access my website, how to detect the user IP? and moreover, how to know which country is the user from? thanks!
Go through with this URL http://beta.codeproject.com/KB/aspnet/aspxcode_net.aspx[^] Let me know if you stuck anywhere.
Parwej Ahamad ahamad.parwej@gmail.com
-
I need to know the user IP to give the right limit to access my website, how to detect the user IP? and moreover, how to know which country is the user from? thanks!
Client IP could be found from
ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
if(ip==string.Empty)
{
ip=Request.ServerVariables("REMOTE_ADDR");
}Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
Client IP could be found from
ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
if(ip==string.Empty)
{
ip=Request.ServerVariables("REMOTE_ADDR");
}Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
Hi, thanks! I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. I saw from internet, there are also others have the same problem. then some people suggest using the following code: string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); but it shows the result as follows: fe70::8c3:258f:3f47:fd6b%12 this is IP?? do you have more comments on this? thank you again!!
-
Go through with this URL http://beta.codeproject.com/KB/aspnet/aspxcode_net.aspx[^] Let me know if you stuck anywhere.
Parwej Ahamad ahamad.parwej@gmail.com
-
Go through with this URL http://beta.codeproject.com/KB/aspnet/aspxcode_net.aspx[^] Let me know if you stuck anywhere.
Parwej Ahamad ahamad.parwej@gmail.com
I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. I saw from internet, there are also others have the same problem. then some people suggest using the following code: string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); but it shows the result as follows: fe70::8c3:258f:3f47:fd6b%12 this is IP?? do you have more comments on this? thank you again!!
-
I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. I saw from internet, there are also others have the same problem. then some people suggest using the following code: string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); but it shows the result as follows: fe70::8c3:258f:3f47:fd6b%12 this is IP?? do you have more comments on this? thank you again!!
I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. Hi you are in right direction and it is correct. It alwasy gives IP: 127.0.0.1 because you are requesting from your local machine. When you will acces you web application via different machine then it will give the correct IP. Anyway your code is correct.
Parwej Ahamad ahamad.parwej@gmail.com
-
I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. Hi you are in right direction and it is correct. It alwasy gives IP: 127.0.0.1 because you are requesting from your local machine. When you will acces you web application via different machine then it will give the correct IP. Anyway your code is correct.
Parwej Ahamad ahamad.parwej@gmail.com
thanks, I see, but do you know string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); gives also IP? or sth else? thank you again!! your article is great!!
-
Hi, thanks! I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. I saw from internet, there are also others have the same problem. then some people suggest using the following code: string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); but it shows the result as follows: fe70::8c3:258f:3f47:fd6b%12 this is IP?? do you have more comments on this? thank you again!!
Seraph_summer wrote:
string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
This is the most funniest code :-D :-D . It means it will try to resolve the dns of the host. Even I saw this. I dont know how people give suggestions to these to resolve IP. .. :laugh: If you are using localhost, the ip will always be 127.0.0.1 as the server and client both the same. Try putting in IIS and get the IP from your local machine. I think you will get the IP address properly. You can also try Request.ServerVariables["REMOTE_HOST"] or Request.UserHostAddress It will give the same output as well. :-D :-D
Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
thanks, I see, but do you know string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); gives also IP? or sth else? thank you again!! your article is great!!
This will give Host IP where your application is deployed. But you are looking for visitor/Client IP.
Parwej Ahamad ahamad.parwej@gmail.com
-
Seraph_summer wrote:
string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
This is the most funniest code :-D :-D . It means it will try to resolve the dns of the host. Even I saw this. I dont know how people give suggestions to these to resolve IP. .. :laugh: If you are using localhost, the ip will always be 127.0.0.1 as the server and client both the same. Try putting in IIS and get the IP from your local machine. I think you will get the IP address properly. You can also try Request.ServerVariables["REMOTE_HOST"] or Request.UserHostAddress It will give the same output as well. :-D :-D
Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
thanks, I see!
-
This will give Host IP where your application is deployed. But you are looking for visitor/Client IP.
Parwej Ahamad ahamad.parwej@gmail.com
really? if so, I am confused by this code. thanks again!
-
thanks, I see, but do you know string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); gives also IP? or sth else? thank you again!! your article is great!!
Seraph_summer wrote:
your article is great!!
At least the Database provided in the article is the most important part of it. Its a great one indeed. :)
Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
really? if so, I am confused by this code. thanks again!
Yes :) Now your next job is to find the country name.
Parwej Ahamad ahamad.parwej@gmail.com
-
thanks, I see!
If the user is behind a proxy then REMOTE_ADDR will be the proxy IP address and HTTP_X_FORWARDED_FOR will be the client IP address. HTTP_X_FORWARDED_FOR can consist of multiple comma separated IP addresses for each proxy the request passes through. Generally, client IP is the first of them. Finally, you can rely on the above methods only for Transparent proxies. There is no way to trace the actual IP in case of Anonymous or Distorting proxies. One more good tool for finding country from an IP is this one[^] on codeplex.