我刚学asp.net,请问如何使用Cookie实现某一个用户登录后显示上次访问时间以及访问次数?麻烦写下关键代码,谢谢
解决方案 »
- asp.net 模板的解析问题?
- 在搜索引擎方面asp.net开发的网站不如php开发的网站好-有图为证
- asp.net中datalist按字段排列
- 编辑数据后如何实现这样的效果?
- 急求关于DATALIST中如何分行显示文本
- 学ASP.NET到底有什么用处?
- 我也来发个招聘启事,请版务保留几日
- asp.net 怎么调用word!!!编辑内容然后打印啊?
- 高高分求支持smtp服务器认证得邮件发送组件 (第一个给我源码人,或可下载的也行就给全部分)
- Response.Redirect能在新窗口中打开吗?
- 我的asp.net应用程序在iis里,怎么获得iis的路径啊?输出图片要。
- 在配置文件下Web.config下有个警告:未声明“inheritInChildApplications”属性
{
Response.Cookies["LastTime"].Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
Response.Cookies["LastTime"].Expires = DateTime.Now.AddDays(60);
int intTotTimes = 0;
if (Request.Cookies["TotTimes"] != null)
{
intTotTimes = Convert.ToInt16(Request.Cookies["TotTimes"].Value); ;
}
intTotTimes = intTotTimes + 1;
Response.Cookies["TotTimes"].Value = intTotTimes.ToString();
Response.Cookies["TotTimes"].Expires = DateTime.Now.AddDays(60); }
protected void Button2_Click(object sender, EventArgs e)
{
if (Request.Cookies["LastTime"] != null)
{
this.TextBox1.Text = Request.Cookies["LastTime"].Value;
}
if (Request.Cookies["TotTimes"] != null)
{
this.TextBox2.Text = Request.Cookies["TotTimes"].Value;
} }
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie); } /// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
/// <param name="expires">过期时间(分钟)</param>
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie); } /// <summary>
/// 读cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <returns>cookie值</returns>
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
{
return HttpContext.Current.Request.Cookies[strName].Value.ToString();
} return "";
}