实现“记录用户登录时间和退出时间,同时记录用户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/

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现用户的登录和登出日志记录,可以使用AOP(面向切面编程)技术,在用户登录退出的时候记录日志。 首先,需要在Spring配置文件中开启AOP支持: ``` <aop:aspectj-autoproxy /> ``` 然后,定义一个切面类来处理登录和登出日志记录: ``` @Aspect public class LoginAspect { @Autowired private HttpServletRequest request; @Autowired private UserService userService; @Pointcut("execution(* com.example.controller.*Controller.login(..))") public void login() {} @Pointcut("execution(* com.example.controller.*Controller.logout(..))") public void logout() {} @AfterReturning("login()") public void afterLogin(JoinPoint joinPoint) { String username = (String) joinPoint.getArgs()[0]; User user = userService.getUserByUsername(username); String ip = request.getRemoteAddr(); String message = "User " + username + " logged in from " + ip; Log log = new Log(user.getId(), message); userService.saveLog(log); } @AfterReturning("logout()") public void afterLogout() { User user = (User) request.getSession().getAttribute("user"); String username = user.getUsername(); String ip = request.getRemoteAddr(); String message = "User " + username + " logged out from " + ip; Log log = new Log(user.getId(), message); userService.saveLog(log); } } ``` 在切面类中定义了两个切点,分别对应用户登录退出的方法。在用户登录后,通过参数获取用户名,再通过UserService获取用户信息和IP地址,最后记录日志。在用户退出后,从Session中获取用户信息,获取IP地址记录日志。 最后,在Spring配置文件中配置切面类: ``` <bean class="com.example.aspect.LoginAspect" /> ``` 这样,在用户登录退出的时候,就会自动记录日志了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值