看了下面的帖子
http://zds420.iteye.com/blog/1244446说是用COOKIE IP地址限制投票次数,它好像是写在jsp页面里面的,不太懂,他给的不完全,不太明白他代码的意思,
是不是还要在数据库里面设计一个ip的字段,然后怎么去限制用户投票的次数啊 。

解决方案 »

  1.   

    常用的投票验证方法:
    IP验证、Cookies验证、Session验证,多种混合验证。以上任何一种验证方法都无法屏蔽刷票机,唯有通过注册用户名,并通过邮箱激活用户名后方可投票,一个用户名只能投一票的方法才能避免机器刷票,但这种方法对用户来说是非常麻烦的。
    通过IP、Cookies混合验证方式:当投票时,在JSP里面获得用户的IP地址、Cookies,然后将IP地址与数据库中的IP地址进行判断,如果数据库中已存在这个IP地址(也就是投过票了),则提示已投过票;IP不同的话,继续进行Cookes验证,首次投票时,设置Cookies的值,当再次投票时,获取Cookies的值进行对比。数据库字段一般设置
    id 自动编号
    IP 投票的IP地址
    voteId  投票项的Id号
    voteTime 投票时间
      

  2.   

    直接在服务端获取用户的ip 
    判断ip投过票就不让投了啊cookie什么的,都在本地是可以被模拟的,不安全
      

  3.   

    当初的JSP网上投票系统也是一个IP30分钟允许投一次。。做法和一楼的类似,但是多了个“下一次投票时间”的字段
      

  4.   


    public class VoteLimitFilter implements Filter { private FilterConfig fc = null; private VoterDao voterDao; public void doFilter(ServletRequest srequest, ServletResponse sresponse,
    FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) srequest;
    HttpServletResponse response = (HttpServletResponse) sresponse;
    HttpSession session = request.getSession(); String dispatch = request.getParameter("dispatch");
    String titleId =request.getParameter("titleId");
    //白名单
    List<String> whitelist = new ArrayList<String>();
    whitelist.add("insertOrUpdateTitle");
    whitelist.add("checkVoteById");
    whitelist.add("showTitleList");
    whitelist.add("prepareNew");
    whitelist.add("showVoteResult");
    whitelist.add("prepareInsertItem");
    whitelist.add("getTitleById");
    whitelist.add("getOptionById");
    whitelist.add("updateItem");
    whitelist.add("insertItem");
    whitelist.add("deleteVote");
    whitelist.add("getVoterList");


    if(!whitelist.contains(dispatch)){
    String ip = request.getRemoteAddr(); // 获取客户端IP
    int optionid = 1;
    // //获取选择的选项ID
    try {
    Date now = new Date(); // 获取当前时间
    Date last = voterDao.getLastVoteTime(ip,Integer.parseInt(titleId)); // 获取该IP的上次对该标题的投票时间
    if (last == null) { // 数据库中没有记录该IP,则该IP地址没有投过票
    addCookie(request, response,titleId); // 在客户端的cookie中添加该用户投票记录
    Voter voter = new Voter();
    voter.setVote_title(Integer.parseInt(titleId));
    voter.setVoter_Ip(ip);
    voter.setVoter_option(optionid);
    voter.setVote_time(StringUtil.timeTostr(now));
    voterDao.saveVoteTime(voter); // 在数据库中记录该IP、选择的选项ID和投票时间
    chain.doFilter(request, response);
    } else { // 该IP地址投过票,则接着判断客户端cookie中是否记录了用户投票情况(用来解决局域网中某个ip投票后,其他ip不能再进行投票的问题)
    boolean voteincookie = seeCookie(request); // 判断当前使用该IP的用户的客户端的cookie中是否记录了投票标记
    if (voteincookie) { // 如果记录了该用户已经投过票
    request.setAttribute("message",
    "● 您已经投过票了,1小时内不允许重复投票!");
    RequestDispatcher rd = request
    .getRequestDispatcher("fail.jsp");
    rd.forward(request, response);
    } else {
    // 没有记录该用户是否投过票,则接着判断当前session中是否记录了用户投票的情况
    // (用来解决用户投票后,删除本地cookie实现重复投票)
    // 用来解决用户投票后,删除本地cookie实现重复投票
    String ido = (String) session.getAttribute("ido");
    if ("yes".equals(ido)) { // 当前用户已投过票
    request.setAttribute("message",
    "● 您已经投过票了,1小时内不允许重复投票!");
    RequestDispatcher rd = request
    .getRequestDispatcher("fail.jsp");
    rd.forward(request, response);
    } else {
    addCookie(request, response,titleId); // 在客户端的cookie中记录该用户已经投过票
    Voter voter = new Voter();
    voter.setVote_title(6);
    voter.setVoter_Ip(ip);
    voter.setVoter_option(optionid);
    voter
    .setVote_time(StringUtil
    .timeTostr(now));
    voterDao.saveVoteTime(voter);
    voterDao.saveVoteTime(voter); // 记录使用该IP的用户的投票时间
    session.setAttribute("ido", "yes");
    chain.doFilter(request, response);
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }else{
    chain.doFilter(request, response);
    }
    } private boolean seeCookie(HttpServletRequest request) {
    boolean hasvote = false;
    String webName = request.getContextPath();
    webName = webName.substring(1);
    String cookiename = webName + ".voter";
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length != 0) {
    for (int i = 0; i < cookies.length; i++) {
    Cookie single = cookies[i];
    if (single.getName().equals(cookiename)
    && single.getValue().equals("I Have Vote")) {
    hasvote = true;
    break;
    }
    }
    }
    return hasvote;
    } private void addCookie(HttpServletRequest request,
    HttpServletResponse response,String titleId) {
    String webname = request.getContextPath();
    webname = webname.substring(1);
    Cookie cookie = new Cookie(webname + titleId+ ".voter", "I Have Vote"); // 创建一个cookie
    cookie.setPath("/");
    // setMaxAge 以秒为单位。
    cookie.setMaxAge(60 * 60 * 1); // 设置cookie在客户端保存的有效时间为1小时
    response.addCookie(cookie); // 向客户端写入cookie
    } public void init(FilterConfig fc) throws ServletException {
    this.fc = fc;
    WebApplicationContext wac = WebApplicationContextUtils
    .getRequiredWebApplicationContext(fc.getServletContext());
    voterDao = (VoterDao) wac.getBean("voterDao");
    } public void destroy() {
    this.fc = null;
    }}
      

  5.   


    30分钟后,你在字段上设置个时间字段不就完了sql也很好写啊select * from note where ip = 'xx.xx.xx.xx' and DATEDIFF(mi, creadte_time, getdate()) > 30
      

  6.   

    回复 8 楼,您说的不太明白,能再具体点吗,在dao层要怎么写啊,以前没遇到过这样的题目,希望您能详细的说说
      

  7.   

    代码都有注释,为什么看不懂?
    在dao层要怎么写啊? 就是取该IP上次投票的时间呗!
    select vote_date from xxx where ip = ?
      

  8.   

    谢谢各位,问题解决了,但是不是12 楼说的,我用的是ip + cookie解决的
      

  9.   


    cookie被人为清除了怎么办?