拿访问网站用户IP 纯JS实现

 后台拿IP 

Java代码 复制代码 收藏代码
  1. String ip = request.getHeader("X-Forwarded-For");
  2. if (ip == null || ip.length() == 0
  3. || "unknown".equalsIgnoreCase(ip)) {
  4. ip = request.getHeader("Proxy-Client-IP");
  5. }
  6. if (ip == null || ip.length() == 0
  7. || "unknown".equalsIgnoreCase(ip)) {
  8. ip = request.getHeader("WL-Proxy-Client-IP");
  9. }
  10. if (ip == null || ip.length() == 0
  11. || "unknown".equalsIgnoreCase(ip)) {
  12. ip = request.getHeader("HTTP_CLIENT_IP");
  13. }
  14. if (ip == null || ip.length() == 0
  15. || "unknown".equalsIgnoreCase(ip)) {
  16. ip = request.getHeader("HTTP_X_FORWARDED_FOR");
  17. }
  18. if (ip == null || ip.length() == 0
  19. || "unknown".equalsIgnoreCase(ip)) {
  20. ip = request.getRemoteAddr();
  21. }
	String ip = request.getHeader("X-Forwarded-For");
	if (ip == null || ip.length() == 0
			|| "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("Proxy-Client-IP");
	}
	if (ip == null || ip.length() == 0
			|| "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("WL-Proxy-Client-IP");
	}
	if (ip == null || ip.length() == 0
			|| "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("HTTP_CLIENT_IP");
	}
	if (ip == null || ip.length() == 0
			|| "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("HTTP_X_FORWARDED_FOR");
	}
	if (ip == null || ip.length() == 0
			|| "unknown".equalsIgnoreCase(ip)) {
		ip = request.getRemoteAddr();
	}

但是由于公司有防火墙 每次都拿滴是防火墙的IP 192.168.92.2 所以根本没有办法统计实际IP 所以上述后台拿IP地址的方法就不行
所以不能给公网地址 ,只能给内网地址(上不了外网) 这样就可以识别内网IP(但是这样外网用户就访问不了)
所以只能做一个判断 发现是防火墙IP 就跳转到内网访问地址。

用JS直接拿IP原理 其实就是通过开发API 在访问我们网站的时候 也访问其它网站 然后拿到后台查询IP地址 然后我们做操作放到前台
下面贴下JS直接拿IP代码
Java代码 复制代码 收藏代码
  1. <script src="http://pv.sohu.com"></script>
  2. document.write(returnCitySN.cip)
<script src=\'#\'" //pv.sohu.com"></script>
document.write(returnCitySN.cip)

直接这样就可以 注意要用sohu的公共API 地址在上一篇文章里面
大家有问题 可以留言
PS:126邮箱有代码  

你可能感兴趣的:(用户,网站)