求session在线人数统计代码在线等解决问题追加30
发布网友
发布时间:2022-04-22 07:54
我来回答
共3个回答
懂视网
时间:2022-04-22 12:16
<?php
ob_start();
session_start();
$num=0;
$dirpt = "online";
$reftime = 1;
if (is_dir($dirpt) && $dir = opendir($dirpt)) {
while (($file = readdir($dir)) !== false) {
if(strcmp($file,"..")==0 || strcmp($file,".")==0){
continue;
}
$D_[date("Y-m-d H:i:s",filemtime($dirpt."/".$file))]=$file;
$num++;
unset($cum);
}
closedir($dir);
$filename = session_id();
$fp = fopen($dirpt."/".$filename,"w");
fputs($fp,"");
fclose($fp);
$ntime = date("Y-m-d H:i:s",mktime(date("H"),date("i")-1,0,date("m"),date("d"),date("Y")));
$D_[$ntime]="-";
krsort($D_);
$onlinenumber=0;
while(1){
$vkey=key($D_);
$onlinenumber++;
if(strcmp($ntime,$vkey)==0){
break;
}else{
array_shift($D_);
}
}
array_shift($D_);
reset($D_);
while(count($D_)>0){
$ckey=key($D_);
unlink($dirpt."/".$D_[$ckey]);
if(!next($D_)){
break;
}
}
}else{
@chmod("..",0777);
@mkdir($dirpt,0777);
}
$online=$onlinenumber-1;
$retime=60*$reftime;
echo "当前在线<strong><font color=red>$online</font></strong>人<meta http-equiv=refresh content="{$retime},url=">";
ob_end_flush();
?>
<!--
《利用文件属性结合Session进行在线人数统计》
作者:sports98
编写日期: 2003-3-19
-->
热心网友
时间:2022-04-22 09:24
void Application_Start(object sender, EventArgs e)
{
//去数据库中读取历史访问总人数.得出总人数acount;
//Application["acount"]=acount;
//如果历史访问人数为0.则:
//Application["acount"]=1;//总访问人数
Application["online"]=1;//当前在线人数
}
void Application_End(object sender, EventArgs e)
{
//将Application["acount"]写入数据库.更新历史访问人数
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
Application.lock();
Application["acount"]=Convert.ToInt32(Application["acount"])+1;//历史访问人数加一
Application["online"]=Convert.ToInt32(Application["online"])+1;//在线总人数加一
Application.Unlock();
}
void Session_End(object sender, EventArgs e)
{
Application.lock();
Application["online"]=Convert.ToInt32(Application["online"])-1;//在线总人数加一
Application.Unlock();
}
lock和Unlock大小写我忘了.你CTRL+J吧.
热心网友
时间:2022-04-22 10:42
我这边有个完整的代码,可以保存历史访问人数和读取历史人数的代码
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
int count = 0;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db_xxx"].ToString());
//这边是数据库连接
try
{
con.Open();
SqlCommand cmd = new SqlCommand("Select count(*) from T_Visit", con);
//这边是访问历史人数表的字段
count = Convert.ToInt32(cmd.ExecuteScalar());
}
catch
{
}
finally
{
con.Close();
}
Application["total"] = count;//Application是个全局变量,每个会话都可对他操作
Application["online"] = 0;
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Session.Timeout = 5;
Application.Lock();//锁定后,只有这个Session能够会话
//执行插入语句
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db_fjdzj"].ToString());
try
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM T_Visit WHERE [VisitIP]=@VisitIP", con);
//这边判断是不是属于同一个人访问,是的话不添加
//我建议把这边替换成Cookies,这样性能更好
cmd.Parameters.Add("VisitIP", SqlDbType.VarChar, 15).Value = Request.UserHostAddress;
SqlDataReader sdr = cmd.ExecuteReader();
DateTime visitDT = DateTime.Now;
while (sdr.Read())
{
visitDT = Convert.ToDateTime(sdr["VisitDateTime"]);
int year = visitDT.Year;
int month = visitDT.Month;
int day = visitDT.Day;
int hour = visitDT.Hour;
int minute = visitDT.Minute;
if ((year == DateTime.Now.Year) && (month == DateTime.Now.Month) && (day == DateTime.Now.Day) && (hour == DateTime.Now.Hour))
{
if ((DateTime.Now.Minute - minute) < 5)
{
sdr.Dispose();
return;
}
}
}
sdr.Dispose();
Application["total"] = (int)Application["total"] + 1;
Application["online"] = (int)Application["online"] + 1;
cmd = new SqlCommand("INSERT INTO T_Visit (VisitDateTime,VisitIP) VALUES (@VisitDateTime,@VisitIP)", con);
cmd.Parameters.Add("VisitDateTime", SqlDbType.DateTime, 8).Value = DateTime.Now.ToLocalTime();
cmd.Parameters.Add("VisitIP", SqlDbType.VarChar, 15).Value = Request.UserHostAddress;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch (Exception ex)
{
}
finally
{
con.Close();
con.Dispose();
}
Application.UnLock();//会话完毕后解锁
}
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
Application.Lock();
if ((int)Application["online"] > 0)
{
Application["online"] = (int)Application["online"] - 1;
}
Application.UnLock();
}
</script>
//Application["total"] 历史访问人数
//Application["online"] 当前在线人数