Click here to Skip to main content
16,016,562 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wanted to get the client IP, below is the working jquery code
HTML
function getIP() {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://jsonip.appspot.com/?callback=DisplayIP";
        document.getElementsByTagName("head")[0].appendChild(script);
    }

   function DisplayIP(response){
       alert(response.ip);
   }


problem is the http://jsonip.appspot.com/?callback=DisplayIP[^] , sometime the website is down, for example now, it said it's over quota. so i wonder is there any possible i can check the client IP without using any outsource?
Posted

Hey Reader,
Use this code of C#, I think it might be helpful to you.
VB
Dim strIpAddress As String = GetIPAddress()
        Dim strgetserverlocation As String = GetServerInfo()
        Dim Datetime__1 As String = Convert.ToString(DateTime.Now)

Public Function GetIPAddress() As String
Try
Dim ipAdd As String
Dim currentRequest As HttpRequest = HttpContext.Current.Request
ipAdd = currentRequest.ServerVariables("HTTP_X_FORWARDED_FOR")

If ipAdd Is Nothing OrElse ipAdd.ToLower() = "unknown" Then
ipAdd = currentRequest.ServerVariables("REMOTE_ADDR")
End If

Return ipAdd
Catch ex As Exception
Throw ex
End Try
End Function
Public Function GetServerInfo() As String
Try
Dim Sinfo As String
Dim currentRequest As HttpRequest = HttpContext.Current.Request
Sinfo = currentRequest.ServerVariables("HTTP_X_FORWARDED_FOR")

If Sinfo Is Nothing OrElse Sinfo.ToLower() = "unknown" Then
Sinfo = currentRequest.ServerVariables("REMOTE_ADDR")
Sinfo += "/" + currentRequest.ServerVariables("LOGON_USER")
End If
Dim computerinfo As String() = Sinfo.Split("/"c)

Return computerinfo(1)
Catch ex As Exception
Throw ex
End Try
End Function
 
Share this answer
 
Comments
melvintcs 16-Sep-13 23:57pm    
this code return the LAN ip address. i need external ip address :)
This code is for both online and Lan also.! because this now running code on website.!!
 
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