做在线投票防止作弊的一些体会
前一阵子因为需要,做一个在线投票的程序,在程序发布以后,一直出现各种bug
一开始是用Cookies做验证,客户禁用Cookies作弊,后来采取记录IP,客户端用软件速度换IP
后来还发现一个问题,就是投票按钮可以重复提交,用户按住投票按钮不放,就可以一直刷票
最后,还是才用Cookies验证的方法
思路是这样的
首先在一个页面产生一个验证码,把它记录到一个Cookies里:Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
在投票按钮事件出发后,首先判断Response.Cookies["CheckCode"]是否==null,如果成立
则提示用户需开启Cookies才能投票,关闭投票小窗口
产生验证码的代码如下:
CheckCode.aspx.cs
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for(int i=0; i<5; i++)
{
number = random.Next();
if(number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if(checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for(int i=0; i<25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for(int i=0; i<100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
在Vote.aspx里引用上页面所产生的验证码
在登录页面中使用“<IMG>” 这个 HTML 元素来显示生成的验证码图片:<IMG src="CheckCode.aspx">
投票按钮事件处理
vote.aspx.cs
private void Button1_Click(object sender, System.EventArgs e)
{
if(Request.Cookies["CheckCode"] == null)
{
Response.Write("<script language=\"JavaScript\">alert('您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能投票!');window.close();</script>");
return;
}
Button1.Enabled = false;
UpdateVote();
}
private void UpdateVote()
{
if(String.Compare(Request.Cookies["CheckCode"].Value, txtValidate.Text, true) != 0)
{
Response.Write(HouseBasic.ScriptAlertMsg("验证码错误,请输入正确的验证码。"));
return;
}
string vote = "," + Request.QueryString["id"];
if(Request.Cookies["Vote"] != null)
{
if(Request.Cookies["Vote"]["IP"] == Request.ServerVariables["REMOTE_ADDR"] && Request.Cookies["Vote"]["ID"].IndexOf(vote) >= 0)
{
Response.Write("<script language=\"JavaScript\">alert('您今天已经为本选手投了一票,不能再投了哟:');window.close();</script>");
Response.End();
}
vote += "," + Request.Cookies["Vote"]["ID"];
}
HttpCookie cookie = new HttpCookie("Vote");
cookie.Values.Add("IP", Request.ServerVariables["REMOTE_ADDR"]);
cookie.Values.Add("ID", vote);
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
//投票处理
}
现在还剩下防止Button按钮重复提交了
在网上搜索了一个防止重复提交的js方法
js.js
function _doPostBack(){};
if(typeof("__doPostBack")=="function")
{
_doPostBack=__doPostBack;
__doPostBack=_doPostBackNew;
}
document.attachEvent("onmousemove",_onmousemove);
var _isPosting=false;
var _divMask=null;
function _onmousemove()
{
if(_divMask)
with(_divMask.runtimeStyle)
{
left=event.clientX+document.body.scrollLeft-4;
top=event.clientY+document.body.scrollTop-4;
}
}
function _makeMask()
{
var div=document.createElement("DIV");
with(div.runtimeStyle)
{
position="absolute";
zIndex=999999;
fontSize="1px";
left=event.clientX+document.body.scrollLeft-4;
top=event.clientY+document.body.scrollTop-4;
width="8px";
height="8px";
cursor="wait";
backgroundColor="gray";
filter="alpha(opacity=10)";
}
try
{
document.body.insertAdjacentElement("BeforeEnd",div);
div.οnblur=new Function("this.focus()");
div.focus();
}
catch(x){}
if(_divMask)_divMask.removeNode(true);
_divMask=div;
}
function _doPostBackNew(sender,args)
{
if(_isPosting)
return event.returnValue=!(event.cancelBubble=true);
status="正在更新页面...";
_doPostBack(sender,args);
_isPosting=true;
_makeMask();
}
function _onformsubmit()
{
if(_isPosting)
return event.returnValue=!(event.cancelBubble=true);
_isPosting=true;
_makeMask();
}
new function _attachForms()
{
with(new Enumerator(document.forms))
for(;!atEnd();moveNext())
{
item().attachEvent("onsubmit",_onformsubmit);
var div=document.createElement("div");
div.runtimeStyle.width="0px";
div.runtimeStyle.hight="0px";
div.runtimeStyle.overflow="hidden";
div.runtimeStyle.position="absolute";
item(0).insertAdjacentElement("afterbegin",div);
div.innerHTML="<INPUT TYPE=Submit name='webformpatchsubmitelement' οnclick='return event.returnValue=false' id='webformpatchsubmitelement' value='webformpatchsubmitelement'/>";
}
}
把这个作为 <-script src=js.js-><-/script-> 的形式Render到每个页面中就可以了。|
如果有PageBase,则在Init的时候用RegisterClientScriptBlock放上去更好。
如:this.RegisterStartupScript("JS","<script src=\"js.js\" type=\"text/javascript\"></script>");
我的这个投票基本上处理,能防止一些常见的作弊方法,欢迎大家提出不足之处
和漏洞
转载于:https://www.cnblogs.com/nonsuch/archive/2006/10/27/541558.html
做在线投票防止作弊的一些体会相关推荐
- python实训项目-Python开发基础-项目实训-在线投票系统.pptx
项目实训-在线投票系统本章任务/30完成"在线投票系统"添加投票候选人删除候选人为候选人投票按序号投票删除投票输出统计信息--本章目标/30理解程序的基本概念会使用顺序.选择.循环 ...
- 用ASP.NET 2.0设计网络在线投票系统
一.系统功能设计和数据库设计 1.系统功能设计和数据库设计 1.1 系统功能设计 网络在线投票系统实现的功能比较简单,具体如下: ◎投票项目的管理: ◎添加投票的项目: ◎删除投票的项目: ◎对项目进 ...
- 状态模式java 在线投票_Java 状态模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述状态(State)模式的:状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为模 ...
- 简易在线投票系统(php)——前端设计(html部分)
简易在线投票系统要求能够支持用户账号的登录和注册,可以进行发起投票.进行投票.查看结果的功能.首先通过前端设计,为各个功能的实现做好准备. 这个项目的初衷只是为了应付学校的检查,所以在前端的设计上,力 ...
- python在线投票系统讲解_Python开发基础-项目实训-在线投票系统ppt课件
<Python开发基础-项目实训-在线投票系统ppt课件>由会员分享,可在线阅读,更多相关<Python开发基础-项目实训-在线投票系统ppt课件(27页珍藏版)>请在人人文库 ...
- 猫鼠游戏,一个刷票老千看在线投票项目的防范与取舍
目录 1.初见老 K 2.与老 K 的对谈 3.老 K 的手段 3.1 cookie 限制 3.2 IP 限制 3.3 签名与验签限制 3.4 验证码限制 3.5 逻辑漏洞 4.拜别老 K 在线网络投 ...
- 软件工程课设--在线投票系统
这次课设我做的是一个在线投票系统,系统功能:登录.注册.浏览投票项目.投票.添加投票. 用户登录 用户注册 投票列表 投票页 新加项目 github:git@github.com:forali/Onl ...
- 在线投票系统的设计与实现
摘 要 随着全球互联网的快速发展和计算机应用的广泛普及,特别是无线网络的广泛覆盖和无线终端设备的爆炸性增长,人们现在可以随时随地访问网络,获取最新信息.参与网络活动和与他人在线互动.为了及时了解民意 ...
- (附源码)springboot在线投票系统 毕业设计 141307
基于springboot在线投票系统 摘 要 随着全球Internet的迅猛发展和计算机应用的普及,特别是近几年无线网络的广阔覆盖以及无线终端设备的爆炸式增长,使得人们能够随时随地的访问网络,以获取最 ...
最新文章
- R语言绘制环形树状图
- ros 配置udev
- 通用扩展函数--类型转换
- Lync Server 2010的部署系列(四) outlook无法加入联机会议
- 反射调用private或internal构造函数
- “如何写好科技论文之我见”结束语-如何写好科技论文之我见(十)
- 工作流表单自定义功能的误区
- 微软为.NET程序员带来了最优的跨平台开发体验-WSL
- LeetCode 84. 柱状图中最大的矩形(单调递增栈)
- 蓬勃发展的社区将帮助您的项目成功的6种方式
- 如何使用CleanMyMac彻底擦除文件?
- VC890D万用表弹片图片
- Ubuntu16.04设置AP热点方法
- Java实现 LeetCode 343 整数拆分(动态规划入门经典)
- 虚拟服务器添加打印机,在VMware Workstation 7虚拟机中应用主机打印机
- 分布式 - 公司使用什么RPC框架,聊聊你理解的RPC原理
- con 元器件符号_关于元器件名称、符号和封装的命名问题
- HapMap-人类基因组单倍型图谱
- 此实现不是 Windows 平台 FIPS 验证的加密算法的一部
- 小妹想学习BI,不知从何下手
热门文章
- Hexo博客添加自定义HTML页面
- solr7.7.2服务启动与停止
- 科普:什么是暴力破解
- 【制作coco数据集】
- SHELL EXECUTE
- centos7.9 部署 zabbix6.0 + mysql (docker-compose)
- 什么是数字签名?什么是数字证书?密码学知识?
- ubuntu16.04下搭建bind9记录
- QTableWidget插入PushButton按键
- mysql indentify_MYSQL Identifying relationship 和non-Identifying relationship 区别