一段能用来统计ip访问的代码(自用)包括所在地

<jsp:useBean id="conn1" scope="session" class="china3cts.SnCms" />
<%
conn1.Conn();
//访问IP
String Ip=request.getRemoteAddr();
//获取IP来源算法
String ip1,ip2,ip3,ip4;

 int x=Ip.indexOf(".");
 ip1=Ip.substring(0,x);
 Ip=Ip.substring(x+1,Ip.length());

 x=Ip.indexOf(".");
 ip2=Ip.substring(0,x);
 Ip=Ip.substring(x+1,Ip.length());

 x=Ip.indexOf(".");
 ip3=Ip.substring(0,x);
 ip4=Ip.substring(x+1,Ip.length());

String Ipone=ip1;
String Iptwo=ip1+"."+ip2;
long IpOK;
IpOK=(long)Integer.valueOf(ip1).intValue()*256*256*256+Integer.valueOf(ip2).intValue()*256*256+Integer.valueOf(ip3).intValue()*256+Integer.valueOf(ip4).intValue();

String SIp="pp";
SIp=Long.toString(IpOK);
String sql="Select Top 1 country,city From sncms_ipaddress Where  ip1<="+SIp+" and ip2>="+SIp+" Order By ip1";
ResultSet rs=conn1.execute(sql) ;

String   country="地区不详";
String   city="地址不详";

if (rs.next())
{
country=rs.getString("country");
city=rs.getString("city");
}

if (city==null)
  city="";

//统计用IP
Ip=request.getRemoteAddr();
//来源
String Reffer=request.getHeader("Referer");
String RootPath=request.getRealPath("/") ;
String URI=request.getRequestURI() ;
String sPath=request.getQueryString() ;
//分割操作系统等信息
String userAgent="";
userAgent=request.getHeader("User-Agent");

String Browser = "unknown";
String os = "unknown";
//浏览器
Browser=userAgent.split(";")[1].trim();
os     =userAgent.split(";")[2].trim();
userAgent=userAgent.toLowerCase();
//操作系统
if (userAgent.indexOf("win") > -1)
{
 
if (userAgent.indexOf("windows nt 5.0") > -1 || userAgent.indexOf("winnt5.0") > -1)
{
  os = "Windows 2000";
}
if (userAgent.indexOf("windows nt 5.1") > -1 || userAgent.indexOf("winnt5.1") > -1)
{
  os = "Windows XP";
}
if (userAgent.indexOf("win16") > -1 || userAgent.indexOf("windows 3.") > -1)
{
  os = "Windows 3.x";
}
}
//统计用代理
userAgent=request.getHeader("User-Agent");

//out.print("IP:"+Ip+"<br>");
//out.print("Reffer:"+Reffer+"<br>");
//out.print("Browser:"+Browser+"<br>");
//out.print("Os:"+os+"<br>");
//out.print("UserAgent:"+userAgent+"<br>");
//out.print("Country:"+country+"<br>");
//out.print("City:"+city+"<br>");
//out.print("RootPath:"+RootPath+"<br>");
//out.print("URI:"+URI+"<br>");
//out.print("sPath:"+sPath+"<br>");
String Uid="test";
//插入数据库
String ISQL=null;
ISQL="insert into SnCms_Logs (Uid,sPat,Pge,ref,Ip,Adr,Bro,Os,Age) values ('"+Uid+"','"+sPath+"','"+URI+"','"+Reffer+"', ";
ISQL=ISQL+"'"+Ip+"','"+country+city+"','"+Browser+"','"+os+"','"+userAgent+"')";
conn1.update(ISQL);
%>

posted @ 2006-10-13 15:14  ddr888  阅读(1394)  评论(5编辑  收藏  举报