投票系統-如何限制單位時間內投票次數
博客分類: 軟件設計
限制對於防止倒票沒有什么絕對的好方法,尤其是用戶不需注冊的情況下的投票,我們來看看有那些方法來防止倒票:
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...哈哈
接下來是常用功能代碼的實現:
- 功能:cookies記錄ip地址,每次檢查先cookies遍歷一遍<pre name="code" class="cpp">(可以設置時間隔多久就可以繼續投)
- 新手注意下面代碼的"///////////////////注釋部分即可" 切勿全部復制然后問我為什么報錯= =
[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 }

