本文主要是介绍IP限制访问量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
private void ValidIPRequest()
{
string ip = "";
string temptime = "";
string time = "";
try
{
ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];//使用代理服务器时,透过代理获得客户机真实IP
if (ip == null)
{
ip = Request.ServerVariables["REMOTE_ADDR"];//当不采用代理时,直接获得客户机IP
}
time = DateTime.Now.ToString("yyyy-MM-dd");//此IP访问时的服务器时间
SqlDBManager db = new SqlDBManager(System.Configuration.ConfigurationSettings.AppSettings["ConnString"].ToString());
System.Data.SqlClient.SqlDataReader sdr = db.Select("select top 1 _time from ipday order by _time desc");
try
{
if (sdr.HasRows)
{
while (sdr.Read())
{
temptime = Convert.ToString(sdr.GetDateTime(0));
}
}
}
finally
{
sdr.Close();
db.ConnClose();
}
if (temptime == null || temptime == "")
{
temptime = time;
}
DateTime dt = Convert.ToDateTime(time);//转换当前服务器时间 为时间型
DateTime tempdt = Convert.ToDateTime(temptime);//转换当前IP数据时间 为时间型
if (dt == tempdt) //服务器时间与IP数据时间相同 说明是当天的点击
{
IPRequest();
}
if (dt > tempdt) //服务器时间>IP数据时间 说明是第二天的点击 获得前一天的点击总数存入iprecord表 并删除ipday中前一天的所有记录
{
int topnum = 0;
System.Data.SqlClient.SqlDataReader sdr1 = db.Select("select sum(num) from ipday");
try
{
if (sdr1.HasRows)
{
while (sdr1.Read())
{
topnum = sdr1.GetInt32(0);
}
}
}
finally
{
sdr1.Close();
db.ConnClose();
}
db.Delete("delete from ipday where _time='" + temptime + "'");
db.Insert("insert into iprecord(_time,topnum) values ('" + temptime + "'," + topnum + ")");
IPRequest();
}
}
catch (Exception ex)
{
throw (ex);
}
}
private void IPRequest()
{
bool b = db.CheckEnable("ip", ip, "ipday");//检验此IP 今天是否访问过 表中存在为false 不存在为true
if (b == false) //此IP今天访问过
{
int n = Convert.ToInt32(db.AnyKey("num", "ip", ip, "ipday"));//获得此IP 今天的访问次数
if (n == 3)
{
Response.Write("<script>alert('你已经点了3次了,一天只能点3次');</script>");
return;
}
else if (n < 3)
{
n++;
db.Update("update ipday set num=" + n);
Response.Write("<script language=javascript>window.open('目标页.aspx');</script>");
}
}
else //此IP今天没有访问过
{
db.Insert("insert into ipday(ip,_time,num) values ('" + ip + "','" + time + "',1)");
Response.Write("<script language=javascript>window.open('目标页.aspx');</script>");
}
}
这篇关于IP限制访问量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!