最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp classic - How to get the IP address of Client Using Javascript - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Nov 19, 2011 at 5:24 casperOne 74.5k19 gold badges189 silver badges260 bronze badges asked Jan 12, 2011 at 15:37 sreekanth sreekanth 7492 gold badges9 silver badges20 bronze badges 4
  • 1 What is your server side language? – Shadow Wizard Commented Jan 12, 2011 at 15:38
  • Duplicate of stackoverflow./questions/391979/… (Unless you have a server-side language, in which case it would be possible, but it wouldn't be only Javascript). – qJake Commented Jan 12, 2011 at 15:38
  • 1 could you give a bit more info about your environment, both above look to be classic ASP snippets. – trickwallett Commented Jan 12, 2011 at 15:39
  • 2 @SpikeX -- the question here looks like the questioner is using JScript in a Classic ASP environment, which makes this a pletely different question than a question about getting the user's IP with client-side JavaScript. – Sean Vieira Commented Jan 12, 2011 at 15:40
Add a ment  | 

7 Answers 7

Reset to default 4

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
%>

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

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.

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 es after the above script tag.

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

try

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

For the record in case anyone is looking for the PHP solution, you'd just 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)

  1. 暂无评论