网络语言中使用的一般技术是什么,允许非注册用户投票?

时间:2011-11-27 09:59:10

标签: web-services web vote

我想了解某些网站使用的一般机制,允许非注册用户或仅仅是客人投票(例如评论,视频,图片等)。他们如何跟踪?与此类网站一样,同一客人不能在同一个终端投票两次。他们存储他们的IP地址吗?或者他们保存计算机ID /名称?

非常感谢任何想法。

P.S :( mywot.com)是此类网站的一个例子。

3 个答案:

答案 0 :(得分:5)

他们使用Cookie或会话来识别已经投票的计算机。如果您对Javascript或PHP有所了解,我可以举几个例子。

编辑: 好的,所以这是一个例子:

<button value="VOTE" onClick="vote();">
<script>
var votes = 9654;  //Some vote counter - only for test purposes - practically, this wouldn't work, the votes would have to be stored somewhere, this number is only stored in the browser and won't actually change for everyone, who sees the page!
function vote()
{
 var cookies = document.cookie.split(";"); //Make an array from the string
 var alreadyVoted = false;
 for (var i = 0; i < cookies.length; i++)  //Iterate through the array
 {
  temp = cookies[i].split("=");
  if (temp[0] == "voted" && temp[1] == "true")  //The cookie is there and it is "true", he voted already
   alreadyVoted = true;
 }
 if (alreadyVoted)
 {
  alert("You can't vote twice, sorry.");
 }
 else
 {
  var date = new Date();
    date.setTime(date.getTime()+(5*24*60*60*1000));  //Cookie will last for five days    (*hours*mins*secs*milisecs)
    var strDate = date.toGMTString();  //Convert to cookie-valid string
  document.cookie = 'voted=true; expires=' + strDate + '; path=/';  //Creating the cookie
  votes++;  //Here would be probably some ajax function to increase the votes number
  alert("Thanks for voting!\nVotes: "+votes);
 }
}
</script>

希望这有帮助,但它只是一个非常简单的cookie demnostration代码,实际上不适用于投票!你必须使用一些PHP或类似的东西来实际存储投票值......

答案 1 :(得分:1)

通常的技术是使用cookies。 IP地址还不够,因为现在很多地方都使用NAT(办公室,网吧,大多数家庭使用DSL路由器)。

理想情况下,Cookie应在后端处理,但也可以在浏览器中使用javascript进行处理。

答案 2 :(得分:1)

Cookie确实是解决方案。这是一个可行的系统:

在数据库中有一个投票表,记住每个访客ID的投票。 当访问者进入您的网站时,请检查该请求是否包含visitor_id Cookie。如果没有,则生成唯一的visitor_id(使用uuid生成器),并在响应中放入visitor_id cookie,并将生成的id作为值。这个cookie应该是持久的。

每次触发投票(和/或每次生成投票链接)时,请检查由其visitor_id cookie标识的当前访问者是否已经投票,这要归功于投票表。投票完成后,将其存储在投票表中。

当然,没有什么能阻止访问者清除其cookie,或使用其他浏览器或机器进行多次投票。但是,如果没有身份验证,那就是你能做到的最好的事情。