我想了解某些网站使用的一般机制,以允许非注册用户或只是访客投票给某些东西(例如评论、视频、图像等)。他们如何跟踪这一点?与在此类站点中一样,同一位访客不能从同一终端投票两次。他们存储他们的 IP 地址吗?或者他们保存计算机 ID/名称?
非常感谢任何想法。
P.S: (mywot.com) 是此类网站的一个例子。
最佳答案
他们使用 cookie 或 session 来识别已经投票的计算机。如果您稍微了解 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 演示代码,实际上不能用于投票!您将不得不使用一些 PHP 或类似的东西来实际存储投票值...
关于web-services - 网络语言中允许非注册用户投票的一般技术是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8284752/