这是一个很典型的jquery里面的getjson方式返回的数据
(我使用的jquery框架,优酷不是使用jq框架,但是原来类似),我又找了一个这个请求的源码:
function vTpListGet(pg, pz, t){ pg = (pg || 1); pz = (pz || 8); t = (t || false); cc = function(oList, total){ if(oList.length > 0){ var html = ""; for(var i=0;i < oList.length;i++){ html += "<ul class=\"x\">\n"; html += " <li class=\"x_thumb\"><a href=\"javascript:;\" onclick=\"vTpSet('"+oList[i].videoid+"','"+oList[i].title+"');\" title=\""+oList[i].title+"\"><img src=\""+oList[i].thumburl+"\" alt=\""+oList[i].title+"\" /></a></li>\n"; html += " <li class=\"x_title\"><a href=\"javascript:;\" onclick=\"vTpSet('"+oList[i].videoid+"','"+oList[i].title+"');\" title=\""+oList[i].title+"\">"+oList[i].title+"</a></li>\n"; html += " <li class=\"x_data\">票数:<span class=\"num\">"+oList[i].total+"</span></li>\n"; html += " <li class=\"x_btn\"><span class=\"btn\" onclick=\"vTpSet('"+oList[i].videoid+"','"+oList[i].title+"');\"></span></li>\n"; html += "</ul>\n"; } html += "<p class=\"clear\"></p>"; //alert(html); document.getElementById('videosTpList').innerHTML = html; if(t){ //显示分页 max_cnt = pz; var js_pager = new jsPager(); js_pager.init(total, pz, pg, "vTpPager"); document.getElementById('videosTpPager').style.display = ""; document.getElementById('videosTpPager').innerHTML = js_pager.getHtml(); } } }; js_request("http://minisite.youku测试数据/pub2/i_am_legend/getvote.php?page="+pg+"&callback=cc&count="+pz+"&i=" + Math.random()); }
再来看优酷关于限制频繁投票的方法:
function vTp(vid){ c = function(num,vid){ alert("投票成功,目前票数为:"+num+"票!"); var exp = new Date (); exp.setTime(exp.getTime() + 3600000); setCookie("nrtp", "true", exp); } if(getCookie("nrtp") != "true"){ js_request("http://minisite.youku测试数据/pub2/i_am_legend/vote.php?id="+vid+"&callback=c&i=" + Math.random()); }else{ alert("一小时内只能投票一次!"); return false; } }
竟然是在客户端写cookies来判断,不禁有点坑爹了,之前我们做网络投票经常被刷票,但是毕竟我们在使用服务端验证,记录一下ip来限制,但是优酷这个投票完全是靠客户端验证来实现。
总结一下优酷的投票:列表页的数据是实时显示的,也就是说投票后立刻显示----我们当年投票也是实时显示,但是服务器压力太大,越到刷票的,数据库被频繁的插入读取,对数据压力也是蛮大的,经常把数据库服务器宕机了,后来采用缓存机制也解决这个问题,一分钟后显示数据。
这个投票的api文件写的有问题,我把http://minisite.youku测试数据/pub2/i_am_legend/vote.php?id=XMjc1NzExMzE2&callback=c&i=0.19621988418141467放在浏览器,不停的刷新居然可以不停的增加票数,很显然优酷的程序员偷懒了,起码也要判断一些提交页面的路径啊,提交方式啊 的什么,判断我是否来自正常的投票请求,如果这样的话这个投票也太容易刷票了,直接把这个url放在浏览器不同的F5就可以了,实在不行直接写个js定时刷新页面也可以。
防止刷票机制,决然是使用客户端cookie做验证,这个就有点菜鸟了,最普通的方式也是根据ip做验证(虽然这个方式在专业刷票公司哪里也是小儿科,但是应付非专业人士还是足够了),防止频繁刷票。
总结;有点失望了,本以为大公司的技术都比我们成熟,看来有点高估了吧,看来不要盲目的迷恋和崇拜大公司,做好自己,相信自己的才是关键!
推荐学习:php视频教程
以上就是大厂是如何做网络投票的?的详细内容!