zoukankan      html  css  js  c++  java
  • asp.net 记录用户打开和关闭页面的时间

    记录打开页面时间:

    在Page_Load事件里记录一下。

    protected void Page_Load(object sender, EventArgs e){

        if (!Page.IsPostBack)
        {

              //记录用户打开页面时间
               Log_User_Time.SetUserTime(Session, Request, Request.Url.PathAndQuery, DateTime.Now, true);

        }

    }

     

    记录关闭页面的时间:

    通过触发页面的onbeforeunload 事件,JS异步提到处理程序,记录时间。

        window.onbeforeunload = LogUserOutTime;
        var requestObj = null;
        function LogUserOutTime() {
            requestObj = getXMLHttpRequest();
            if (!!requestObj) {          
                requestObj.open("GET", "/LogUserOutTime.ashx", true);
                requestObj.onreadystatechange = reqend;
                requestObj.send(null);
            }
        }

        function reqend() {        
        }

        function getXMLHttpRequest() {
            var xmlRequestObj = null;
            try {
                if (window.ActiveXObject) {
                    // Internet Explorer
                    try {
                        xmlRequestObj = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            xmlRequestObj = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {
                            throw e;
                        }
                    }
                } else if (window.XMLHttpRequest) {
                    // Firefox, Opera 8.0+, Safari
                    xmlRequestObj = new window.XMLHttpRequest();
                }
            } catch (e) {
            }
            return xmlRequestObj;
        }

     

    其它的代码:

    LogUserOutTime.ashx 如下所示:

    <%@ WebHandler Language="C#" class="LogUserOutTime" %>

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.SessionState;
    using nnbh.BaseComm.CommonClass;
    using System.Text;

    /// <summary>
    /// 记录用户操作时间
    /// </summary>
    public class LogUserOutTime : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetNoStore();  //设置为不缓存      
            
            Log_User_Time.SetUserTime(context.Session, context.Request, context.Request.UrlReferrer.PathAndQuery, DateTime.Now, false);

            context.Response.Clear();
            context.Response.End();
        }

        #endregion
    }

     

    此方法在关闭页面和关闭浏览器时都会触发。

  • 相关阅读:
    Photoshop给草坡上的人物加上大气的霞光
    Photoshop给人像加上个性裂纹肌肤
    Photoshop快速给美女人像换头发
    Oracle和SQLite位数不足补0
    Oracle和Postgis数据库地理坐标系下面积计算
    SQLite3中自增主键归零方法
    Arcgis-Tools_06矢量数据按国土分解
    Arcgis-Issues_03Arcgis最佳路径分析
    Arcgis-Tools_05批量导出地图
    Oracle_C#连接Oracle数据库
  • 原文地址:https://www.cnblogs.com/moweiran/p/3501266.html
Copyright © 2011-2022 走看看