`
zds420
  • 浏览: 197987 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

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%>
 
分享到:
评论
2 楼 zds420 2012-09-21  
Zhang987526341 写道
求lz 详细说说,我复制您的代码后,要怎么改啊 。。初学者,求详细解释

抱歉,最近繁忙,未能及时看到留言,我这是把简单的代码提出来了,还有数据库表没贴出来,主要就是根据IP地址限制投票,还要新建一个IP地址库。
1 楼 Zhang987526341 2012-08-27  
求lz 详细说说,我复制您的代码后,要怎么改啊 。。初学者,求详细解释

相关推荐

Global site tag (gtag.js) - Google Analytics