实现“记录用户登录时间和退出时间,同时记录用户IP,并且要记录用户IP对应的省和城市地址”
想实现“记录用户登录时间和退出时间,同时记录用户IP,并且要记录用户IP对应的省和城市地址”
用Session_Start和Session_End,实现是最初的想法,结果不理想。
主要是时间不准确,不能将TimeOut时间设得太短了。
关键过程:
新建一aspx页面-spyWin.aspx
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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();
}
}
}
{
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();
}
}
}
完成上述关键(非全部) 步骤后,在真正的网站母版页里加上
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<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>
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);
登录页面加上:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<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>
</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://four-corner.appspot.com/