当前位置: 首页 > news >正文

投票系统之防止重复投票

投票系统-如何限制单位时间内投票次数


博客分类: 软件设计
限制对于防止倒票没有什么绝对的好方法,尤其是用户不需注册的情况下的投票,我们来看看有那些方法来防止倒票:


1.Session  采用Session对象防止重复投票好像还不错,如果您利用单一浏览器进行测试,确实可以证明Session具有防止重复投票的功能,实际上开启另一个浏览器,Session变了,那么又可以投票了.为什么呢?因为每一个执行中的浏览器对应一个Session对象,虽然我们可以设置第一个浏览器的Session值,但是第二
个.第三个.....无法设置了..


2.Cookie   一般利用Cookie进行设置,主要是设置Cookie的失效时间,也就是在这段时间内,这台电脑的信息被Cookie保存,你可以做允许的事情,这样我们可以利用其进行投票,比如说登录的时候将Client的IP地址赋值给Cookie,Cookies("Value").Expires="12/31/2999";用户登录的时候,我们检查Cookie是否有值,来决
定他是否有权限进行投票.这种方法比Session应该好多了,重启,开启多个浏览器都被Cookie左右,但是致命的一项是Cookie是可以清除的,这样我们的设置又轻易的被破解了.


3.IP+数据库 这是目前还算有效但是不是绝对有效的方法,下面的示例将记录我做的教师测评的限制IP的源码.用户登录的时候,取得Client端的IP,并且与系统数据库存储的IP比较(系统存储的数据可以按照时间的
降序排列,这样如果有重复IP,我们只比较最上面的那条就可以了,具体看代码!):如果相同的话,再次比较时间,如果两者时间差超过半小时则可以投票,否则警告信息:一台电脑半小时内
只能投票一次;如果不相同的话,就是说明这个IP没有投票过,那么可以进行投票,同时更新IP和时间纪录!这种方法也有一致命的漏洞---动态IP地址,比如ADSL还有其它的动态变化的IP等等,这样也就失去作用
了(由于我们学校是静态IP,所以我这样做啦,o(∩_∩)o...哈哈).


4.IP+Cookie 这种方法又多了一层保障,但是对于动态IP地址+删除Cookie的组合来说还是可以破解的.


5.Mac 网卡的物理地址在世界唯一,我们可以通过网卡的Mac地址(物理地址)来进行锁定电脑,这方法看起来不错,但是很多软件都能制造伪Mac地址....


6.就是用户注册ID投票,这样限制一个ID只能投票一次或者单位时间内只能投1次效果是非常好的,但是一个人也可以注册很多用户ID啊!!所以上述6中方法没有一种是100%有效的方法,大家根据自己所需,按照自己的要求选择,所谓防君子,不防

小人嘛o(∩_∩)o...哈哈


接下来是常用功能代码的实现:

[cpp] view plain copy
  1. </pre><pre name="code" class="cpp">功能:同一用户帐号投票完就不能继续投票(我这里是利用数据库的一个字段,解决session的一些弊端而且这个字段还能用于其他地方)  
[cpp] view plain copy
  1. 功能:cookies记录ip地址,每次检查先cookies遍历一遍<pre name="code" class="cpp">(可以设置时间隔多久就可以继续投)  
[cpp] view plain copy
  1. 新手注意下面代码的"///注释部分即可" 切勿全部复制然后问我为什么报错= =  

[cpp] view plain copy

 

  1     package servlets;  
  2       
  3     import java.io.IOException;  
  4     import java.io.PrintWriter;  
  5     import java.text.NumberFormat;  
  6     import java.util.ArrayList;  
  7     import java.util.List;  
  8       
  9     import javax.servlet.ServletContext;  
 10     import javax.servlet.ServletException;  
 11     import javax.servlet.http.Cookie;  
 12     import javax.servlet.http.HttpServlet;  
 13     import javax.servlet.http.HttpServletRequest;  
 14     import javax.servlet.http.HttpServletResponse;  
 15     import javax.servlet.http.HttpSession;  
 16       
 17     import dao.impl.VoteDaoImpl;  
 18     import dao.impl.VoteInfoDaoImpl;  
 19       
 20     import service.IVoteService;  
 21     import values.ApplyValue;  
 22     import values.VoteInfoValue;  
 23     import vote.dao.IVoteDao;  
 24     import vote.dao.VoteInfoDao;  
 25     import vote.show.VoteInfo;  
 26     import vote.utils.ConvertUtil;  
 27       
 28       
 29     public class VoteServlet extends HttpServlet {  
 30       
 31         /** 
 32          * Destruction of the servlet. <br> 
 33          */  
 34         public void destroy() {  
 35             super.destroy(); // Just puts "destroy" string in log  
 36             // Put your code here  
 37         }  
 38       
 39         public void doGet(HttpServletRequest request, HttpServletResponse response)  
 40                 throws ServletException, IOException {  
 41       
 42             response.setContentType("text/html;charset=utf-8");  
 43             //PrintWriter out = response.getWriter();  
 44             String method = request.getParameter("method");  
 45             System.out.println("method="+method);  
 46             //response.setCharacterEncoding("utf-8");  
 47             HttpSession session = request.getSession();  
 48               
 49             String userid=(String) session.getAttribute("userid");  
 50               
 51             System.out.println("userid="+userid);  
 52             PrintWriter out = response.getWriter();  
 53             ConvertUtil cutils = new ConvertUtil();  
 54             List listVote = new ArrayList();  
 55             //业务处理层  
 56             //IVoteService ivote = new VoteServiceImpl();  
 57             IVoteDao ivote =  new VoteDaoImpl();  
 58             VoteInfoDao vid=new VoteInfoDaoImpl();  
 59             String tourl = "";  
 60             // 判断请求是否为投票操作。  
 61     //      if ("addvote".equals(method)) {  
 62     //          String vname = request.getParameter("vname");  
 63     //          ivote.insertVote(vname);  
 64     //          tourl = "vote.do?method=votemanage";  
 65     //      } else   
 66                 if ("voting".equals(method)) {  
 67     //          if (session.getAttribute("times") == null) {  
 68                     //用方法类将取得的String变为int  
 69                     //ServletContext application=session.getServletContext();  
 70                     //String usernam=(String) application.getAttribute("usernam");  
 71                     //String userid=request.getParameter("id");  
 72                       
 73                     //System.out.println("userid="+userid);  
 74                     boolean temp=false;  
 75                             temp=vid.searchName(userid);  
 76                     String IP = request.getRemoteHost();  
 77                     System.out.println(IP);  
 78                     Cookie[] cookies = request.getCookies();/取得所有cookies  
 79                       
 80                     boolean flag = true;  
 81       
 82                     for (int i = 0; i < cookies.length; i++) {  
 83       
 84                       if (IP.equals(cookies[i].getValue())) {  
 85     <span style="white-space:pre">                                </span>//ip验证  
 86                         flag = false;  
 87                         break;  
 88       
 89                       }}  
 90                     System.out.println("flag="+flag);  
 91                     System.out.println("temp="+temp);  
 92                     if(flag&&(!temp)){ //双重保险  
 93                     System.out.println("检测用的");  
 94                         String voteids1[] = request.getParameterValues("voteid1"); //得到被投票人的id  
 95                         //String voteids2[] = request.getParameterValues("voteid2");  
 96                         //解决空指针异常问题  
 97                         if(voteids1==null) {  
 98                             String mess = "<script>alert('您没有选择任何投票项目,请返回后重新投票谢谢!');</script>";  
 99                             request.setAttribute("mess", mess);  
100                             tourl = "/GetDBtoShow?mymethon=tovote";  
101                             request.getRequestDispatcher(tourl).forward(request, response);  
102                             return;  
103                         }  
104                         //通过这里控制能得到的投票个数  
105                         System.out.println("voteid="+voteids1[0]);  
106                         if (voteids1.length <=2&&voteids1.length>0) {  
107                             System.out.println("获得的长度>0");  
108                             ivote.updataVcount(voteids1);  
109       
110                               
111                               
112                             vid.insertData(userid, voteids1);  
113       
114                             //session.setAttribute("times", "do");  
115                             //mess作为js  
116                             String mess = "<script>alert('投票成功,谢谢支持!!');</script>";  
117                              Cookie cookie = new Cookie("IP", IP); /存ip  
118     //                       cookie.setMaxAge(30);//存放30天够多的了= =  
119                                 cookie.setMaxAge(60*60*24*30);//存放30天够多的了= =  
120       
121                                 response.addCookie(cookie);//回写到浏览器  
122                                 //application.setAttribute("usernam", userid);  
123                             request.setAttribute("mess", mess);  
124                         }   
125                         else if(voteids1.length>2){  
126                             String mess = "<script>alert('选项不能多于两个=。=');</script>";  
127                             request.setAttribute("mess", mess);  
128                         }  
129                           
130                           
131       
132                     }   
133                 else {  
134                     String mess = "<script>alert('您已经投过票,请不要重复投票!');</script>";  
135                     request.setAttribute("mess", mess);  
136                 }  
137                 tourl = "/GetDBtoShow?mymethon=tovote";  
138             } else if ("view".equals(method)) {  
139                 //显示第一个页面  
140                 int totalnum1 = ivote.totalVote_1();//得到总票数  
141     //          int totalnum2 = ivote.totalVote_23();//得到总票数  
142                 List list1 = ivote.getVoteList1();//得到满足isok的人  
143     //          List list2 = ivote.getVoteList2_3();//得到满足isok的人  
144                 List voteList1 = new ArrayList();  
145     //          List voteList2 = new ArrayList();  
146                 if (list1 != null) {  
147                     for (int i = 0; i < list1.size(); i++) {  
148                         if(totalnum1==0){totalnum1=1;}  
149                         ApplyValue vote =  (ApplyValue) list1.get(i);//得到第 i个对象  
150                         double ii = (double) vote.getVcount() / totalnum1 * 100;  
151                         NumberFormat formatter = NumberFormat.getNumberInstance();  
152                         double bfb=(double) vote.getVcount() / totalnum1;  
153                         formatter.setMaximumFractionDigits(2);  
154                         String vs = formatter.format(ii) + "%";//投票占百分比  
155                         int width = vote.getVcount() * 100 / totalnum1;  
156                         VoteInfo voteinfo = new VoteInfo();  
157                         voteinfo.setNumber(i + 1);  
158                         //voteinfo.setVotename(vote.getA_name());  
159                         voteinfo.setVotename(vote.getA_name());  
160                         voteinfo.setCount(vote.getVcount());  
161                         voteinfo.setBfb(bfb);//double 百分比  
162                         voteinfo.setVs(vs);//投票占百分比  
163                         voteinfo.setWidth(width);  
164                         voteList1.add(voteinfo);  
165                     }  
166                     request.setAttribute("voteList1", voteList1);  
167                     //tourl = "/GetDBtoShow?mymethon=toview";  
168                     //tourl = "/WEB-INF/view.jsp";  
169                 }  
170     //          if (list2 != null) {  
171     //              for (int i = 0; i < list2.size(); i++) {  
172     //                  ApplyValue vote =  (ApplyValue) list2.get(i);//得到第 i个对象  
173     //                  double ii = (double) vote.getVcount() / totalnum2 * 100;  
174     //                  NumberFormat formatter = NumberFormat.getNumberInstance();  
175     //                  formatter.setMaximumFractionDigits(2);  
176     //                  String vs = formatter.format(ii) + "%";//投票占百分比  
177     //                  int width = vote.getVcount() * 100 / totalnum2;  
178     //                  VoteInfo voteinfo = new VoteInfo();  
179     //                  voteinfo.setNumber(i + 1);  
180     //                  //voteinfo.setVotename(vote.getA_name());  
181     //                  voteinfo.setVotename(vote.getA_name());  
182     //                  voteinfo.setCount(vote.getVcount());  
183     //                  voteinfo.setVs(vs);//投票占百分比  
184     //                  voteinfo.setWidth(width);  
185     //                  voteList2.add(voteinfo);  
186     //              }  
187     //              request.setAttribute("voteList2", voteList2);  
188     //              //tourl = "/GetDBtoShow?mymethon=toview";  
189     //                
190     //          }  
191     //显示第二个页面  
192                 List voteinfolist=vid.getVoteList();  
193                 List voteinL = new ArrayList();  
194                   
195                     if (voteinfolist != null) {  
196                         for (int i = 0; i < voteinfolist.size(); i++) {  
197                             VoteInfoValue votevalue=(VoteInfoValue) voteinfolist.get(i);  
198                             voteinL.add(votevalue);  
199                             }  
200                         request.setAttribute("voteinL", voteinL);  
201                         }  
202                 System.out.println("实现了第二页面的初始化");  
203                 tourl = "/WEB-INF/NewView.jsp";  
204             }  
205             else if ("showuservote".equals(method)) {  
206                 List voteinfolist=vid.getVoteList();  
207                 List voteinL = new ArrayList();  
208                   
209                     if (voteinfolist != null) {  
210                         for (int i = 0; i < voteinfolist.size(); i++) {  
211                             VoteInfoValue votevalue=(VoteInfoValue) voteinfolist.get(i);  
212                             voteinL.add(votevalue);  
213                             }  
214                         request.setAttribute("voteinL", voteinL);  
215                         }  
216                 System.out.println("实现了第二页面的初始化");  
217                 tourl = "/WEB-INF/UserVoteInfo.jsp";  
218             }  
219             else if ("votemanage".equals(method)) {  
220                 List list = ivote.getVoteList();  
221                 request.setAttribute("votelist", list);  
222                 tourl = "mainvote.jsp";  
223             } else if ("edit".equals(method)) {  
224                 ApplyValue vote = new ApplyValue();  
225                 int id = cutils.strToInt(request.getParameter("id"));  
226                 String vname = request.getParameter("vname");  
227                 int vcount = cutils.strToInt(request.getParameter("vcount"));  
228                 vote.setA_id(id);  
229                 vote.setA_name(vname);  
230                 vote.setVcount(vcount);  
231                 if (ivote.updataVoteByVote(vote)) {  
232                     request.setAttribute("mess", "编辑成功");  
233                 } else {  
234                     request.setAttribute("mess", "编辑失败");  
235                 }  
236                 tourl = "result.jsp";  
237             }   
238     //      else if ("toedit".equals(method)) {  
239     //          int tmpid = cutils.strToInt(request.getParameter("id"));  
240     //          ApplyValue vote = ivote.getVoteById(tmpid);  
241     //          request.setAttribute("vote", vote);  
242     //          tourl = "voteEdit.jsp";  
243     //      }  
244         else if ("delete".equals(method)) {  
245                 int id = cutils.strToInt(request.getParameter("id"));  
246                 ivote.delete(id);  
247                 tourl = "vote.do?method=votemanage";  
248             } else {  
249                 tourl = "vote.do?method=votemanage";  
250             }  
251             request.getRequestDispatcher(tourl).forward(request, response);  
252         }  
253       
254         public void doPost(HttpServletRequest request, HttpServletResponse response)  
255                 throws ServletException, IOException {  
256       
257             this.doGet(request, response);  
258         }  
259       
260         /** 
261          * Initialization of the servlet. <br> 
262          * 
263          * @throws ServletException if an error occurs 
264          */  
265         public void init() throws ServletException {  
266             // Put your code here  
267         }  
268       
269     }  

 

相关文章:

  • python redis使用心得
  • Tools - Markdown
  • 深入理解ajax系列第一篇——XHR对象
  • 如果图片链接找不到地址出错自动显示默认图片(头像)
  • 现代化敏捷简介
  • HTML link标签media参数
  • mongodb中数据类型的坑
  • Velocity初探小结--Velocity在spring中的配置和使用
  • 图像金字塔
  • 如何学习linux的建议
  • Scrapy ImagePipeline(图片下载组件)
  • Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext
  • Java读取表格数据
  • 将js对象转为json对象属性加上引号
  • 【Augmented Reality】增强现实中的光学透射式头盔显示器的标定进阶
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • 30天自制操作系统-2
  • Java Agent 学习笔记
  • JavaScript标准库系列——Math对象和Date对象(二)
  • JavaScript创建对象的四种方式
  • Java超时控制的实现
  • JS 面试题总结
  • MySQL QA
  • MySQL几个简单SQL的优化
  • PHP 小技巧
  • Rancher如何对接Ceph-RBD块存储
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 消息队列系列二(IOT中消息队列的应用)
  • 小程序 setData 学问多
  • 学习HTTP相关知识笔记
  • - 语言经验 - 《c++的高性能内存管理库tcmalloc和jemalloc》
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • HanLP分词命名实体提取详解
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​决定德拉瓦州地区版图的关键历史事件
  • # 安徽锐锋科技IDMS系统简介
  • #DBA杂记1
  • #stm32整理(一)flash读写
  • #我与Java虚拟机的故事#连载04:一本让自己没面子的书
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (java)关于Thread的挂起和恢复
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (二)Eureka服务搭建,服务注册,服务发现
  • (二)换源+apt-get基础配置+搜狗拼音
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (附源码)计算机毕业设计SSM保险客户管理系统
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (一)基于IDEA的JAVA基础10
  • (转)创业家杂志:UCWEB天使第一步
  • (转)大道至简,职场上做人做事做管理
  • .NET Core中的去虚
  • .net refrector
  • .Net Remoting(分离服务程序实现) - Part.3