| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1653 人关注过本帖
标题:网站浏览总次数次数和最高在线人数怎么做
只看楼主 加入收藏
ilovetea
Rank: 1
来 自:辽宁省锦州市
等 级:新手上路
帖 子:177
专家分:4
注 册:2008-3-31
结帖率:100%
收藏
 问题点数:0 回复次数:5 
网站浏览总次数次数和最高在线人数怎么做
最好是绑定数据库的方法
搜索更多相关主题的帖子: 数据库 浏览 绑定 
2008-04-14 14:54
yunj1105
Rank: 1
等 级:新手上路
威 望:1
帖 子:757
专家分:0
注 册:2007-2-13
收藏
得分:0 
网站浏览总次数次数和最高在线人数
建立一个Global.asax里面代码如下:
<%@ Application Language="C#" %>
<%@ Import Namespace=" %>
<%@ Import Namespace="System.Data" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        Application["UsersOnline"] = 0;
        int count = 0;
        StreamReader srd;
        //取得文件的实际路径
        string file_path = Server.MapPath("counter.txt");
        //打开文件进行读取
        srd = File.OpenText(file_path);
        while (srd.Peek() != -1)
        {
            string str = srd.ReadLine();
            count = Int32.Parse(str);
        }
        object obj = count;
        Application["TotalCount"] = obj;
        srd.Close();
    }
   
    void Application_End(object sender, EventArgs e)
    {
        int count = 0;
        count = (int)Application["TotalCount"];
        string file_path = Server.MapPath("counter.txt");
        StreamWriter fs = new StreamWriter(file_path, false);
        fs.WriteLine(count);
        fs.Close();

        //DataSet objDataSet = new DataSet();
        //objDataSet.ReadXml(Server.MapPath("count.xml"));
        //int maxCount = Convert.ToInt32(objDataSet.Tables["MyCount"].Rows[0]["MaxCount"]);
        //Application["MaxCount"] = maxCount;
        //if (maxCount < (int)Application["UsersOnline"])    //如果MaxCount标签里面的值小于当前在线人数的值,就存入
        //    objDataSet.Tables["MyCount"].Rows[0]["MaxCount"] = (int)Application["UsersOnline"];
        //objDataSet.WriteXml(Server.MapPath("count.xml"));
    }
        
    void Application_Error(object sender, EventArgs e)
    {
        // 在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e)
    {
        Application.Lock();
        Application["UsersOnline"] = (int)Application["UsersOnline"] + 1;

        StreamReader srd;
        int maxCount = 0;
        //取得文件的实际路径
        string file_pathm = Server.MapPath("max.txt");
        //打开文件进行读取
        srd = File.OpenText(file_pathm);
        while (srd.Peek() != -1)
        {
            string str = srd.ReadLine();
            maxCount = Int32.Parse(str);
        }
        srd.Close();
        if (maxCount < (int)Application["UsersOnline"])
            maxCount = (int)Application["UsersOnline"];
        string max_file_path = Server.MapPath("max.txt");
        StreamWriter max_fs = new StreamWriter(max_file_path, false);
        max_fs.WriteLine(maxCount);
        max_fs.Close();
        
        //访问总数加1
        int count = 0;
        count = (int)Application["TotalCount"];
        count = count + 1;
        Application["TotalCount"] = count;
        //将数据记录写入文件
        string file_path = Server.MapPath("counter.txt");
        try
        {
            StreamWriter fs = new StreamWriter(file_path, false);
            fs.WriteLine(count);
            fs.Close();
        }
        catch (Exception ex)
        {
            Application["Error"] = ex.Message;
        }
        Application.UnLock();
    }

    void Session_End(object sender, EventArgs e)
    {
        Application["UsersOnline"] = (int)Application["UsersOnline"] - 1;
        Application.UnLock();
    }
      
</script>
使用时:
 protected void Page_Load(object sender, EventArgs e)
    {
        if (Application["UsersOnline"] != null)
        {
            currentUserNum.Text = Application["UsersOnline"].ToString();
        }
        if (Application["TotalCount"] != null)
        {
            totalUserNum.Text = Application["TotalCount"].ToString();
        }
        if (Application["UsersOnline"] != null)
        {
            StreamReader srd;
            int maxCount = 0;
            //取得文件的实际路径
            string file_path = Server.MapPath("max.txt");
            //打开文件进行读取
            srd = File.OpenText(file_path);
            while (srd.Peek() != -1)
            {
                string str = srd.ReadLine();
                maxCount = Int32.Parse(str);
            }
            srd.Close();
            if (maxCount < int.Parse(Application["UsersOnline"].ToString()))
                maxCount = int.Parse(Application["UsersOnline"].ToString());
            maxUserNum.Text = maxCount.ToString();
        }
根据需要建txt文件

2008-04-14 15:05
ilovetea
Rank: 1
来 自:辽宁省锦州市
等 级:新手上路
帖 子:177
专家分:4
注 册:2008-3-31
收藏
得分:0 
真好啊, 我去尝试下

我是爱喝茶的胖子,有空一起喝茶啊
我的百度博客:http://hi.baidu.com/33806380
2008-04-14 15:13
残影留香
Rank: 1
来 自:广西南宁
等 级:新手上路
帖 子:193
专家分:0
注 册:2008-3-19
收藏
得分:0 
嗯,不错,学习中^
2008-04-14 15:35
guiqian145
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-11-9
收藏
得分:0 
好东西
2008-04-15 10:53
shezhenhui1989
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:287
专家分:0
注 册:2008-2-1
收藏
得分:0 
学习学习
2008-04-15 10:55
快速回复:网站浏览总次数次数和最高在线人数怎么做
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042294 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved