|
楼主 |
发表于 2004-5-30 16:02:35
|
显示全部楼层
这个是用数据库来处理的。
我从一个论坛的代码里面找出来的一个方法。
[PHP]
public static void checkUserStats(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, String s)
{
String s1 = GCookie.getCookieValue(httpservletrequest, "UJBBUName", "");
String s2 = GCookie.getCookieValue(httpservletrequest, "UJBBUPSW", "");
String s3 = httpservletrequest.getRemoteAddr();
String as[] = IPLocalizer.search(httpservletrequest);
String s4 = "";
for(int i = 0; i < as.length; i++)
s4 = s4 + as;
String s5 = httpservletrequest.getHeader("X_FORWARDED_FOR");
long l = Long.parseLong(StringUtils.replace(s3, ".", ""));
boolean flag = false;
String s6 = httpservletrequest.getHeader("User-Agent");
try
{
String s7 = "";
DBConnect dbconnect = new DBConnect();
boolean flag1;
try
{
s7 = "select * from online where id=?";
dbconnect.prepareStatement(s7);
dbconnect.setLong(1, l);
ResultSet resultset = dbconnect.executeQuery();
if(resultset.next())
flag1 = true;
else
flag1 = false;
resultset.close();
}
catch(Exception exception1)
{
flag1 = false;
}
if("".equals(s1))
{
if(!flag1)
{
s7 = "insert into online (id,username,userclass,ip,startime,lastimebk,browser,stats,actforip,ComeFrom,actCome) values(?,?,?,?,?,?,?,?,?,?,?)";
dbconnect.prepareStatement(s7);
dbconnect.setLong(1, l);
dbconnect.setString(2, "guest");
dbconnect.setBytes(3, "客人".getBytes("GBK"));
dbconnect.setBytes(4, s3.getBytes("GBK"));
dbconnect.setString(5, Format.getDateTime());
dbconnect.setString(6, Format.getDateTime());
dbconnect.setString(7, s6);
dbconnect.setBytes(8, s.getBytes("GBK"));
dbconnect.setString(9, s5);
dbconnect.setBytes(10, s4.getBytes("GBK"));
dbconnect.setString(11, s5);
dbconnect.executeUpdate();
dbconnect.clearParameters();
} else
{
s7 = "update online set lastimebk=?,lastime=?,ComeFrom=?,actCome=?,stats=? where id=?";
dbconnect.prepareStatement(s7);
dbconnect.setString(1, Format.getDateTime());
dbconnect.setString(2, Format.getStrDateTime());
dbconnect.setBytes(3, s4.getBytes("GBK"));
dbconnect.setString(4, s5);
dbconnect.setBytes(5, s.getBytes("GBK"));
dbconnect.setLong(6, l);
dbconnect.executeUpdate();
dbconnect.clearParameters();
}
} else
{
s7 = "select * from online where ID=? or username=?";
dbconnect.prepareStatement(s7);
dbconnect.setLong(1, l);
dbconnect.setBytes(2, s1.getBytes("GBK"));
ResultSet resultset1 = dbconnect.executeQuery();
boolean flag2;
if(resultset1.next())
flag2 = true;
else
flag2 = false;
dbconnect.clearParameters();
try
{
User user = new User(s1, s2, 4);
if(flag2)
{
s7 = "update online set id=?,userName=?,userClass=?,lastimebk=?,lastime=?,ComeFrom=?,actCome=?,stats=? where id=? or username=?";
dbconnect.prepareStatement(s7);
dbconnect.setLong(1, l);
dbconnect.setBytes(2, s1.getBytes("GBK"));
dbconnect.setBytes(3, getUserClass(user.getUserClass()).getBytes("GBK"));
dbconnect.setString(4, Format.getDateTime());
dbconnect.setString(5, Format.getStrDateTime());
dbconnect.setBytes(6, s4.getBytes("GBK"));
dbconnect.setString(7, s5);
dbconnect.setBytes(8, s.getBytes("GBK"));
dbconnect.setLong(9, l);
dbconnect.setBytes(10, s1.getBytes("GBK"));
dbconnect.executeUpdate();
dbconnect.clearParameters();
} else
{
s7 = "insert into online(id,username,userclass,ip,startime,lastimebk,browser,stats,actforip,ComeFrom,actCome) values(?,?,?,?,?,?,?,?,?,?,?)";
dbconnect.prepareStatement(s7);
dbconnect.setLong(1, l);
dbconnect.setBytes(2, s1.getBytes("GBK"));
dbconnect.setBytes(3, getUserClass(user.getUserClass()).getBytes("GBK"));
dbconnect.setString(4, s3);
dbconnect.setString(5, Format.getDateTime());
dbconnect.setString(6, Format.getStrDateTime());
dbconnect.setString(7, s6);
dbconnect.setBytes(8, s.getBytes("GBK"));
if(s5 == null)
{
dbconnect.setString(9, "null");
dbconnect.setString(11, "null");
} else
{
dbconnect.setString(9, s5);
dbconnect.setString(11, s5);
}
dbconnect.setBytes(10, s4.getBytes("GBK"));
dbconnect.executeUpdate();
dbconnect.clearParameters();
}
}
catch(Exception exception2)
{
if(!flag2)
{
s7 = "insert into online (id,username,userclass,ip,startime,lastimebk,browser,stats,actforip,ComeFrom,actCome) values(?,?,?,?,?,?,?,?,?,?,?)";
dbconnect.prepareStatement(s7);
dbconnect.setLong(1, l);
dbconnect.setString(2, "guest");
dbconnect.setBytes(3, "客人".getBytes("GBK"));
dbconnect.setBytes(4, s3.getBytes("GBK"));
dbconnect.setString(5, Format.getDateTime());
dbconnect.setString(6, Format.getDateTime());
dbconnect.setString(7, s6);
dbconnect.setBytes(8, s.getBytes("GBK"));
dbconnect.setString(9, s5);
dbconnect.setBytes(10, s4.getBytes("GBK"));
dbconnect.setString(11, s5);
dbconnect.executeUpdate();
dbconnect.clearParameters();
} else
{
s7 = "update online set lastimebk=?,lastime=?,ComeFrom=?,actCome=?,stats=? where id=?";
dbconnect.prepareStatement(s7);
dbconnect.setString(1, Format.getDateTime());
dbconnect.setString(2, Format.getStrDateTime());
dbconnect.setBytes(3, s4.getBytes("GBK"));
dbconnect.setString(4, s5);
dbconnect.setBytes(5, s.getBytes("GBK"));
dbconnect.setLong(6, l);
dbconnect.executeUpdate();
dbconnect.clearParameters();
}
}
}
s7 = "delete from online where now()>date_add(lastimebk,interval 20 minute)";
dbconnect.executeUpdate(s7);
s7 = "select Maxonline from config";
ResultSet resultset2 = dbconnect.executeQuery(s7);
resultset2.next();
int j = resultset2.getInt(1);
s7 = "select count(*) from online";
ResultSet resultset3 = dbconnect.executeQuery(s7);
resultset3.next();
int k = resultset3.getInt(1);
if(k > j)
{
String s8 = "update config set Maxonline=" + k + ",MaxonlineDate=now()";
dbconnect.executeUpdate(s8);
ForumPropertiesManager.resetManager();
}
dbconnect.close();
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
[/PHP] |
|