简体   繁体   中英

I have fetch the ip address of my website visitor, how can i get their geographic location too?

I want to get the geographic location of my website visitors and store it in my database, have fetch the ip address already.

I'm using asp.net 3.0

protected void Page_Load( object sender, EventArgs e)
    {
        // Track Visitors
        string ipAddress = IpAddress();
        string hostName = Dns .GetHostByAddres(ipAddress).HostName;
        StreamWriter wrtr = new StreamWriter (Server.MapPath( "visitors.log" ), true );
        wrtr.WriteLine( DateTime .Now.ToString() + " | " + ipAddress + " | " + hostName + " | " + Request.Url.ToString());
        wrtr.Close();
    }

    private string IpAddress()
    {
        string strIpAddress;
        strIpAddress = Request.ServerVariables[ "HTTP_X_FORWARDED_FOR" ];
        if (strIpAddress == null )
            strIpAddress = Request.ServerVariables[ "REMOTE_ADDR" ];
        return strIpAddress;
    }

There is no inbuilt method to that. There are web-services on the internet that will provide such mapping. Such databases "GeoIP databases" are also available for local use.

But it would be best to subscribe to an updated web-service and use that to get the location. Google's Client Location API 's should help you here.

Some modern browsers will send you the users location. You can use that. But since this is not followed by all browsers, it is not reliable. You can check out the Geo Location API provided by w3.org.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM