ASP实例:限制ip投票

由于工作关系,要做个人空间,其中有一功能是对用户投票,要求同一IP只能对同一用户投票一次,贴Asp代码:

<%
'作者:无情 出处:http://www.dwww.cn
db_conn(dbs)
Voteusername=trim(request.QueryString("username"))
rs_create("select username from [user] where username ='"&Voteusername&"'")
if rs.eof and rs.bof then
response.write "错误的参数,请从正确访问!"
response.end()
end if

'第一种情况是第一次点击,cookies为空,ip为空
'第二种情况是点第二个人投票,第一个人的cookies存在,第二个的的cookies不存在,但是ip存在
'第三种情况是换ip投票,cookies存在,ip为空

if Request.cookies("dwww")(""&Voteusername&"")="" then
Response.cookies("dwww")(""&Voteusername&"")=Voteusername&"|"
Response.cookies("dwww").Expires=Date()+365
Call VoteBody()
else
if instr(request.cookies("dwww")(""&Voteusername&""),request.cookies("dwww")(""&Voteusername&"")&"|")<>0 then
Call VoteBody()
else
'response.write "对不起,您已经投过票了!Cookies重复"
Response.Write "<script>alert('对不起,您已经投过票了!');history.back();</script>"
response.end()
end if
end if

Sub VoteBody()
Voteusername=trim(request.QueryString("username"))
CheckIp = Request.ServerVariables("HTTP_X_FORWARDED_FOR") '绕过代理IP
If CheckIp = "" Then CheckIp=Request.ServerVariables("REMOTE_ADDR")
GetUrl=""
GetUrl=Request.ServerVariables("Http_Referer")

sql="Select username,ip from vote where username= '"&Voteusername&"' and ip='"&CheckIp&"'"
rs_create(Sql)
If Not rs.Bof And Not rs.eof Then
'response.write "对不起,您已经投过票了!IP重复"
Response.Write "<script>alert('对不起,您已经投过票了!');history.back();</script>"
response.end()
Else
conn.execute ("insert into vote (username,ip,addtime) values ('"&Voteusername&"','"&CheckIp&"','"&now()&"')")
conn.execute ("update [user] set vote=vote+1 where username ='"&Voteusername&"'")
'response.write "投票成功!"
Response.Write "<script>alert('恭喜您,投票成功!');location.href='"&Request.ServerVariables("HTTP_REFERER")&"'</script>"
response.end()
End If
end Sub

rs_close()
db_close()
%>

写的不好,见笑了.

出处:http://dwww.cn/News/2007-12/200712191627276172.shtml

你可能感兴趣的:(asp)