实现“记录用户登录时间和退出时间,同时记录用户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();
}
}
}
{
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>
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>
</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/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
2009-04-08 将使用AJAX的DNN模块部署到.net 2.0站点上的问题
2009-04-08 DNN中使用XSD dataset之tableadapter出错
2009-04-08 vs2008中,不能将控件拖到页面上之问题解决