How to get client IP address using Javascript

Hi, please let me know how I can get the client IP address. I used the following.

Ip = <--#echo var="REMOTE_ADDR"-->;

ip = '<%= Request.UserHostAddress>';

But they do not work.

+3
source share
7 answers

Most web servers (I assume you are using IIS) provide this information in the REMOTE_ADDR Environment Variable .

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

+2
source

IP- ASP Classic

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

:

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

, .asp, IP-.

+2

JavaScript, , , .

, ( ) IP s * erver * script , .

, , .

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

USER_IP , , , , script.

+1

ip = '<%= Request.UserHostAddress %>';
0

You cannot get IP using pure Javascript. The two solutions you provide require server-side scripting.

0
source

For the record, in case someone is looking for a PHP solution, you simply do it like this:

<?php echo $_SERVER['REMOTE_ADDR']; ?>

For example, if you were passing it in a JS array, it would look like this:

tr._setSomeVar(5, "Remote Address", "<?php echo $_SERVER['REMOTE_ADDR']; ?>", 9);
0
source

Source: https://habr.com/ru/post/1784990/


All Articles