ASP.NET设置发帖时间间隔不超过30秒
IT专家网 > winsystem
ASP.NET设置发帖时间间隔不超过30秒
正在做一个站,有论坛功能,为了避免发帖机或者恶意灌水,需要这样的限制。
以下是代码片段:
protected void btnPost_Click(object sender, EventArgs e)
{
if (Session["lastTime"] == null)
{
Session["lastTime"] = DateTime.Now;
}
DateTime dt = Convert.ToDateTime(Session["lastTime"]);
TimeSpan ts = DateTime.Now - dt;
Response.Write(ts.Days + ":" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds);
if (ts.Hours > 0 || ts.Minutes > 0 || ts.Seconds > 30)
{
Session["lastTime"] = DateTime.Now;
Response.Write("可以发帖!");
}
else
{
Response.Write("你的发帖速度过快!时间间隔不要小于30秒!");
}
}
/// <summary>
/// 把秒转换成分钟
/// </summary>
/// <returns></returns>
public static int SecondToMinute(int Second)
{
decimal mm = (decimal)((decimal)Second / (decimal)60);
return Convert.ToInt32(Math.Ceiling(mm));
}
/// <summary>
/// 计算两个时间的时间间隔
/// </summary>
/// <param>较早的日期和时间</param>
/// <param>较后的日期和时间</param>
/// <returns></returns>
public static string DateDiff(DateTime DateTimeOld, DateTime DateTimeNew)
{
string dateDiff = "";
TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks);
TimeSpan ts2 = new TimeSpan(DateTimeNew.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
int day = ts.Days;
int hou = ts.Hours;
int minu = ts.Minutes;
int sec = ts.Seconds;
if (day > 0)
{
if (day > 30)
{
if (day > 364)
{
dateDiff += day / 365 + "年";
}
else
{
dateDiff += day / 30 + "个月";
}
}
else
{
dateDiff += day.ToString() + "天";
}
}
else
{
if (hou > 0)
{
dateDiff += hou.ToString() + "小时";
}
else
{
if (minu > 0)
{
dateDiff += minu.ToString() + "分钟";
}
else
{
if (sec > 0)
{
dateDiff += sec.ToString() + "秒";
}
else
{
dateDiff += "0秒";
}
}
}
}
if (DateTimeNew.CompareTo(DateTimeOld) > 0)
{
dateDiff += "前";
}
else
{
dateDiff += "后";
}
return dateDiff;
}
/// <summary>
///判断是否于5分钟之前
/// </summary>
/// <param>较早的日期和时间</param>
/// <returns></returns>
public static bool DateDiff_minu(DateTime DateTimeOld)
{
TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
int minu = ts.Minutes;
if (minu > 5)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 与当前时间比较,重载时间比较函数,只有一个参数
/// </summary>
/// <param>较早的日期和时间</param>
/// <returns></returns>
public static string DateDiff(DateTime DateTimeOld)
{
return DateDiff(DateTimeOld, DateTime.Now);
}
/// <summary>
/// 日期比较,返回精确的几分几秒
/// </summary>
/// <param>较早的日期和时间</param>
/// <param>较迟的日期和时间</param>
/// <returns></returns>
public static string DateDiff_full(DateTime DateTime1, DateTime DateTime2)
{
string dateDiff = null;
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
return dateDiff;
}
/// <summary>
/// 时间比较,返回精确的几秒
/// </summary>
/// <param>较早的日期和时间</param>
/// <param>较迟的日期和时间</param>
/// <returns></returns>
public static int DateDiff_Sec(DateTime DateTime1, DateTime DateTime2)
{
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
int dateDiff = ts.Days * 86400 + ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
return dateDiff;
}
/// <summary>
/// 日期比较
/// </summary>
/// <param>当前日期</param>
/// <param>输入日期</param>
/// <param>比较天数</param>
/// <returns>大于天数返回true,小于返回false</returns>
public static bool CompareDate(string today, string writeDate, int n)
{
DateTime Today = Convert.ToDateTime(today);
DateTime WriteDate = Convert.ToDateTime(writeDate);
WriteDate = WriteDate.AddDays(n);
if (Today >= WriteDate)
return false;
else
return true;
}
相关文章
关键词:ASP.NET,.Net,微软技术
责任编辑:valen
专题推荐
原创文章
微博互动
白皮书
All Rights Reserved, Copyright 2004-2013, Ctocio.com.cn
渝ICP证B2-20030003号 如有意见请与我们联系 powered by 天极内容管理平台CMS4i