JSP实现获取网页访问者IP

JSP的办法

假如用代理服务器,request.getRemoteAddr();   只能得到代理服务器地址,以下方法是得到正确所有ip的方法
在jsp声明这个函数
<%!public String getIpAddr(HttpServletRequestrequest) { 
    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.getRemoteAddr();    
      
    returnip;    
}%>
嵌入jsp网页中,然后用<%Stringip=getIpAddr(request);%>得到访问者ip
ip就是访问者的ip,你保存到数据库或者缓存中,就由你自己了


其他语言的资料

function GetClientIP()
IPaddress =Request.ServerVariables("HTTP_X_FORWARDED_FOR") 
If IPaddress = "" Then IPaddress =Request.ServerVariables("REMOTE_ADDR")
GetClientIP=IPaddress
end function

response.write GetClientIP() 'IP
response.write now() '访问时间

 

 Request.ServerVariables("HTTP_X_FORWARDED_FOR")这个是在有代理的情况下获取真实IP
加入没有代理
就用Request.ServerVariables("REMOTE_ADDR") 

所以链接起来写就像楼上那样就可以了


JSP中可以使用request.getRemoteAddr()获得IP地址,request.getRemoteHost()获得主机名


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值