实现“记录用户登录时间和退出时间,同时记录用户IP,并且要记录用户IP对应的省和城市地址”

想实现“记录用户登录时间和退出时间,同时记录用户IP,并且要记录用户IP对应的省和城市地址”

 

用Session_Start和Session_End,实现是最初的想法,结果不理想。

 

主要是时间不准确,不能将TimeOut时间设得太短了。

 

关键过程:

 

新建一aspx页面-spyWin.aspx

 

代码
  protected   void  Page_Load( object  sender, EventArgs e)
    {
        
        Common.LogOut();

         string  script  =   " <script type='text/javascript'  "   +
                 " language='javascript'>  "   +
                 "  function check_opener() {  "   +

                 "  parent.opener='';  "   +
                 "  parent.close();  "   +

                 " }  "   +
                 "  onload = function() {  "   +
                 "  self.blur();  "   +
                 "  setTimeout('check_opener()',0);  "   +
                 "  }  "   +
                 "  </script> " ;
         this .Controls.Add( new  LiteralControl(script));

    }

// 以上是spyWin.aspx的代码

// 以下是Common类的代码
  public   static   void  LogLogin( string  address)
    {
        
        MyBusiness.DbModel.LogForSale lfs  =   new  LogForSale();
        lfs.LoginTime  =  DateTime.Now;
        lfs.UserName  =  Credential.Name;

         if  (address.Length  >   1 )
        {
         lfs.IP =    address.Split( ' , ' )[ 0 ];
         lfs.Province  =  address.Split( ' , ' )[ 2 ];
         lfs.City  =  address.Split( ' , ' )[ 3 ];
        }
        
        
        lfs.Save();

        System.Web.HttpContext.Current.Session[ " LFS " ]  =  lfs;
    }
    
    
     public   static   void  LogOut()
    {
         if  (Credential  !=   null )
        {
            MyBusiness.DbModel.LogForSale lfs  =  System.Web.HttpContext.Current.Session[ " LFS " ]  as  MyBusiness.DbModel.LogForSale;
             if  (lfs != null && lfs.ID  >   0 )
            {
                lfs.LogoutTime  =  DateTime.Now;
                lfs.Save();
            }
        }
    }

 

 

完成上述关键(非全部) 步骤后,在真正的网站母版页里加上

代码
     < script language = " javascript " >
function  launch_spyWin() 
{
    spyWin  =  open( ' spyWin.aspx ' , ' spyWin ' ,
        ' width=100,height=100,left=2000,top=0,status=0 ' );
    spyWin.blur();
}
onunload  =  launch_spyWin;



     < / script>

 

 

为了记录登录时间等,需要在登录的事件里添加

 

 Common.LogLogin(TextBoxLog.Text); 

 

 

 登录页面加上:

代码
     < asp:TextBox  runat ="server"  ID ="TextBoxLog"  CssClass ="TextBoxLog Element-Hide" ></ asp:TextBox >
     </ form >
</ body >
<% --// 2010 - 4 - 8  LogForSale 起 -- %>

< script  type ="text/javascript"  src ="http://fw.qq.com/ipaddress"  charset ="gb2312" ></ script >

< script  type ="text/javascript" >  
 
$( ' .TextBoxLog ' ).val((IPData.join( ' , ' )));

</ script >

 

 

 

http://anforen.5d6d.com/

 

http://four-corner.appspot.com/

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值