Home >Web Front-end >JS Tutorial >JavaScript method to obtain client IP (new method)_javascript skills

JavaScript method to obtain client IP (new method)_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:11:051934browse

For a long time, I have used http://fw.qq.com/ipaddress to get the IP of the client user. This method is simple, fast and practical.

The way we call it is:

<script type="text/javascript" src="http://fw.qq.com/ipaddress"></script> 

It can return user IP and location, metaphor:

var IPData = new Array("220.181.108.85","","北京市",""); 

We can now use IPData[0] as the user IP.

But I don’t know what happened to Tencent recently. This location has failed, prompting "HTTP 500 Internal Server Failure".

Another way to get the client IP using js

Recently, I learned another way to get the client IP. This service is provided by an overseas website.

The application method is as follows:

<script language="JavaScript"> 
VIH_BackColor = "palegreen"; 
VIH_ForeColor = "navy"; 
VIH_FontPix = "16"; 
VIH_DisplayFormat = "You are visiting from:
IP Address: %%IP%%
Host: %%HOST%%"; 
VIH_DisplayOnPage = "yes"; 
</script> 
<script language="JavaScript" src="http://www.hashemian.com/js/visitorIP.js.php"></script> 

The following is your IP:

You are visiting from: IP Address: 117.87.4.102 Host: 102.4.87.117.broad.xz.js.dynamic.163data.com.cn

If you don’t want to display it on the web page, but just want to obtain the IP for other purposes, you need to change VIH_DisplayOnPage = "yes" to VIH_DisplayOnPage = "no". The IP variable is VIH_HostIP.

The calling method is as follows:

<script language="JavaScript"> 
VIH_DisplayOnPage = "no"; 
</script> 
<script language="JavaScript" src="http://scripts.hashemian.com/js/visitorIPHOST.js.php"></script> 
<script language="JavaScript"> 
alert("你的IP是:" + VIH_HostIP); 
</script> 

Let me introduce JavaScript to obtain the client IP and MAC address

If you cannot obtain it, you need to set up the IE browser. The specific settings are as follows:

Please set the third item of IE browser-Tools-Security-Custom Level-ActiveX to enable 'Initialize and script run ActiveX controls not marked as safe', click OK, refresh and log in again!

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaScript获取客户端IP</title>
<script type="text/javascript" language="javascript">
<!--
function GetLocalIPAddress()
{
var obj = null;
var rslt = "";
try
{
obj = new ActiveXObject("rcbdyctl.Setting");
rslt = obj.GetIPAddress;
obj = null;
}
catch(e)
{
//异常发生
}
return rslt;
}
function getMac(){
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled =True");
var e = new Enumerator (properties);
{
var p = e.item();
var mac = p.MACAddress;
return mac
}
}
//-->
function init () {
var ip = GetLocalIPAddress();
var mac = getMac();
fm.clientIP.value = ip;
fm.clientMAC.value = mac;
} 
</script>
</head>
<body onload="init();">
<form name = 'fm'>
通过JavaScript获取的IP信息:<input type ='text' name = 'clientIP'><br>
通过JavaScript获取的MAC信息:<input type='text'name = 'clientMAC'> 
通过JSP获取的IP信息:<%= request.getRemoteAddr() %>
</form>
</body>
</html>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn