COOKIE IP地址限制投票次数

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.saas.query.baseQueryConf.JDBCExceute" %>
<%! 
public String getIpAddr(HttpServletRequest request) { 
    String ip = request.getHeader("x-forwarded-for"); 
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
        ip = request.getHeader("Proxy-Client-IP"); 
    } 
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
        ip = request.getHeader("WL-Proxy-Client-IP"); 
    } 
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
        ip = request.getRemoteAddr(); 
    } 
    return ip; 
} 

public String getCookieIPAddress(HttpServletRequest request,HttpServletResponse response,String ip_address) {
	Cookie[] cookies = request.getCookies();
	String cookievalue ="";
	if(null!=cookies && cookies.length!=0) {
		for(int i=0;i<cookies.length;i++) {
			Cookie cookie = cookies[i];
			if(cookie.getName().equals("cookie_ip_"+ip_address)) {
				String value= cookie.getValue();
				String[] value_array=value.split("\\|");
				int time= Integer.parseInt(value_array[1])+1;
				cookievalue = value_array[0]+"|"+time;//取出原来cookie中的信息,在后面追加信息    
                Cookie c = new Cookie("cookie_ip_"+ip_address,cookievalue);  //编码后添加都cookie    
                c.setMaxAge(60 * 60);    
                c.setPath("/");    
                response.addCookie(c); 
			}
			else {
				Cookie c = new Cookie("cookie_ip_"+ip_address,ip_address+"|1");
				c.setMaxAge(24*60*60);
				c.setPath("/");
				response.addCookie(c);
			}
			System.out.println(cookievalue);
		}
	}
	return cookievalue;
}
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String article_id = request.getParameter("article_id")!=null ? request.getParameter("article_id").toString() : "";
String ip_address = getIpAddr(request);

String vote_stats="";

String cookie_ip = getCookieIPAddress(request,response,ip_address);

String[] cookie_ip_array = cookie_ip.split("\\|");

System.out.println(cookie_ip_array[1]);
if(!"".equals(cookie_ip_array[1].toString().trim()) && Integer.parseInt(cookie_ip_array[1])>=20) {
	vote_stats="每个IP地址每天限投20票";
}
else {
	JDBCExceute jdbc = new JDBCExceute();
	jdbc.CRUD("update ith_article set rsrv_str4=rsrv_str4+1 where article_id='"+article_id+"'");
	vote_stats="投票成功";
}
%>
<%=vote_stats%>
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值