How to get the IP address of Client Using Javascript

19,882

Solution 1

Most web servers (I'm assuming you're using IIS) provide this information in the REMOTE_ADDR Environment Variable.

Your samples are trying to get at the server's variables with Classic ASP and Server Side Includes, both of which are turned off by default on modern IIS web servers. You may need to enable classic ASP or SSI, or use the ServerVariables property using ASP.NET

Solution 2

Use the following code for to get the IP Address in ASP Classic

<%
Dim UserIPAddress
UserIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If UserIPAddress = "" Then
UserIPAddress = Request.ServerVariables("REMOTE_ADDR")
End If
%>

Solution 3

Try this:

<script type="text/javascript">
    var ip = "<%=Request.ServerVariables("REMOTE_ADDR")%>";
    alert(ip);
</script>

Assuming this is .asp page you should see alert with your ip address.

Solution 4

You cannot do this with JavaScript alone, it looks to me like you're trying to use server side code on the client, that doesn't work.

The easiest, fastest (and ugliest) of doing this would be to output the IP on the s*erver side*, into a script tag on the page that's being send to the user.

No idea which language you're using on the server side, so I'll provide you some pseudo code.

echo '<script type="text/javascript">var USER_IP = ' + getRemoteAddess() + ';</script>'

This will introduce a global variable called USER_IP into the page, make sure that the code that uses this variable comes after the above script tag.

Share:
19,882
sreekanth
Author by

sreekanth

HI I am sreekanth i am dotnet professtional

Updated on June 04, 2022

Comments

  • sreekanth
    sreekanth almost 2 years

    Hi Please let me know how can i get the client IP address. I have used the following .

    Ip = <--#echo var="REMOTE_ADDR"-->;
    
    ip = '<%= Request.UserHostAddress>';
    

    But these are not working.