Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to get user's

Ip address,
city,
state,
country,
longitude and
latitude

when loged in in my website,

then how can i get that data....

please help me...

want source....
thnx
Posted
Updated 16-Aug-17 22:58pm
v2
Comments
PKriyshnA 26-Jan-12 8:51am    
i got ip address using

string visitorIpAddress = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
visitorIpAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
lblIp.Text = visitorIpAddress;
}
else
{
visitorIpAddress = HttpContext.Current.Request["REMOTE_ADDR"];
lblIp.Text = visitorIpAddress;
}
PKriyshnA 26-Jan-12 9:05am    
help any one please.....
PKriyshnA 27-Jan-12 10:31am    
i get api key and can get information in iplocators webpage

in

OK;;27.121.105.12;AU;AUSTRALIA;NEW SOUTH WALES;PYRMONT;-;-33.869;151.194;+10:00

format

using

http://api.ipinfodb.com/v3/ip-city/?key=&ip=27.121.105.12

but how can i impliment in my webpage....
but how can i read data and store in my label
PKriyshnA 27-Jan-12 10:49am    
how can i read data from that web page and store in one variable,

please help....

I agree with Marcus and to help the OP a bit here is one link:

http://www.mycsharpcorner.com/Post.aspx?postID=27[^]
 
Share this answer
 
Comments
Lakamraju Raghuram 26-Jan-12 9:50am    
And also this may help

http://www.dotnetcurry.com/ShowArticle.aspx?ID=325
Lakamraju Raghuram 26-Jan-12 9:51am    
ha! just came to know that this question is duplicate and the above link is already provided by 'Dalek Dave'
Espen Harlinn 26-Jan-12 10:15am    
5'ed!
You can't. There is no inherent correlation between an IP address and the physical location of the client.

The best you can get is the geographic location of the registered supplier of the IP address. That will generally resolve to a city, but it may be several hundred miles from the actual location of the client.

However, it is possible, but most of the suppliers will cost you money. Google[^] And I suggest you check first,. as the quality of the information varies a lot. (My IP resolves to about 25 miles away on some, "Unknown city" on others.)
 
Share this answer
 
Comments
Espen Harlinn 26-Jan-12 10:15am    
5'ed!
Use HostIp.Info[^] and get the Geo API.
Or use any of the webservices available.


This[^] may help.
 
Share this answer
 
Comments
Espen Harlinn 26-Jan-12 10:15am    
5'ed!
Michael Gr 26-Jan-12 16:51pm    
I use the HostIP with MySQL.
You'll need to download their IP Addresses Database and then refresh it from time to time: http://www.hostip.info/dl/index.html
Their API is quite simple as well: http://www.hostip.info/use.html
Ip is the internet protocol for communication between nodes.
Ip is used to identify a host and address the location. If we need to identify the user
who are all accessing our website and store the ip address in our database is very simple and very easier.
This is the way to track the users ip address. Fetch a client's ip address as soon as he access our web site in asp.net.
Some of them may use a proxy ip address. But we can get their ip address with this simple code.

Add following namespace in page
using System.Net;

C#
string ipAddress ="";

//Get the Host Name
string hostName = Dns.GetHostName();
//Get The Ip Host Entry
IPHostEntry ipHostEntry = Dns.GetHostEntry(hostName);
//Get The Ip Address From The Ip Host Entry Address List
IPAddress[] ipAddress = ipHostEntry.AddressList;
ipAddress = ipAddress[ipAddress.Length - 1].ToString();


To get the Geolocation of IP address you can use the various API which would gives you result in various different format(csv,xml)
http://www.ipinfodb.com/ip_location_api.php

You can get Geolocation information from
http://api.ipinfodb.com/v2/ip_query.php?key=<your_api_key>&ip=74.125.45.100&timezone=false

Response comes in XML format. using xml deserialize, I deserialize the response.
[XmlRootAttribute(ElementName = "Response", IsNullable = false)]
C#
public class IPLocator
{

private string longitude;
public string Longitude
{
get { return longitude; }
set { longitude = value; }
}

private string latitude;
public string Latitude
{
get { return latitude; }
set { latitude = value; }
}

private string zip;
public string Zip
{
get { return zip; }
set { zip = value; }
}

private string ip;
public string IP
{
get { return ip; }
set { ip = value; }
}
}

After deserialization IPLocater class bind All properties of requested IP Address.

Binding Class is return IPLocater class.


Code of IPDetals Class
C#
public IPLocator GetData(string ipAddress)
    {
        IPLocator ipLoc = new IPLocator();
        try
        {
            //apiKey can be generated from below link
            //http://www.ipinfodb.com/ip_location_api.php
            string apiKey = "anykey";

            string path = "http://api.ipinfodb.com/v2/ip_query.php?key=" + apiKey + "&ip=" + ipAddress + "&timezone=false";
            WebClient client = new WebClient();
            string[] eResult = client.DownloadString(path).ToString().Split(',');
            if (eResult.Length > 0)
                ipLoc = (IPLocator)Deserialize(eResult[0].ToString());
        }
        catch
        { }
        return ipLoc;
    }


//Desrialize XML String

private Object Deserialize(String pXmlizedString)
{
XmlSerializer xs = new XmlSerializer(typeof(IPLocator));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}

//String to UTF8ByteArray
private Byte[] StringToUTF8ByteArray(String pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
}


//You can get the Geolocation infoamation here
string ipAddress = HttpContext.Current.Request.UserHostAddress;
IPDetails ipDetails=new IPDetails ();
IPLocator ipLocater = ipDetails.GetData(ipAddress);
Response.Write(ipLocater.CountryName);
 
Share this answer
 
Comments
PKriyshnA 27-Jan-12 9:29am    
i get api key and can get information in iplocators webpage

in

OK;;27.121.105.12;AU;AUSTRALIA;NEW SOUTH WALES;PYRMONT;-;-33.869;151.194;+10:00

format

using

http://api.ipinfodb.com/v3/ip-city/?key=<my_key>&ip=27.121.105.12

but how can i impliment in my webpage....
PKriyshnA 27-Jan-12 11:51am    
how can i configure ....

still did not get solution...hope u will help
Mukund Thakker 30-Jan-12 0:40am    
Just write the following code in your webpage. It will provide your requested information.

//You can get the Geolocation infoamation here
string ipAddress = HttpContext.Current.Request.UserHostAddress;
IPDetails ipDetails=new IPDetails ();
IPLocator ipLocater = ipDetails.GetData(ipAddress);
Response.Write(ipLocater.CountryName);
You aren't really showing us much initiative in solving your own problem, but I won't leave you completely out in the cold.
Do a search based on "C# how to get location from ip address" and check out the results. Because you just want someone to dump the code for you, I'm not even going to linkify the search like I usually do.
 
Share this answer
 
Comments
Espen Harlinn 26-Jan-12 10:15am    
5'ed!
Slacker007 28-Jan-12 2:05am    
Not much of an answer, Marcus. Answer the question or pass, don't lecture.
fjdiewornncalwe 28-Jan-12 10:25am    
Most questions of this nature I would actually just give the OP the correct search term and a link to the results with none of the lecture, but in this case it is a recurring theme with this OP which is why, granted out of frustration, I answered the way it is. You are fully entitled and I certainly don't disagree with the downvote on this either. Cheers.

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