由于出于安全性问题,javascript并不能直接获得客户端的IP。
但是后端语言确实可以的。
所以javascript可以通过ajax技术来获得后端语言得到的IP,并返回到前端。
1:通过java的servlet获取IP:
public String getRemoteAddress(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { ip = request.getRemoteAddr(); } return ip; }
如果是用反向代理技术的话request.getHeader("x-forwarded-for");会有值,
一般情况下,只要request.getRemoteAddr();就可以了。
2:获取MAC:
public String getMACAddress(String ip) { String str = ""; String macAddress = ""; try { Process p = Runtime.getRuntime().exec("nbtstat -a " + ip); InputStreamReader ir = new InputStreamReader(p.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i < 100; i++) { str = input.readLine(); if (str != null) { //if (str.indexOf("MAC Address") > 1) { if (str.indexOf("MAC") > 1) { macAddress = str.substring( str.indexOf("=") + 2, str.length()); break; } } } } catch (IOException e) { e.printStackTrace(System.out); } return macAddress; }
通过命令nbtstat -a ip来获得该ip相对应的mac地址。
注意:验证时url时不要用localhost或者127.0.0.1,要使用ip的方式。