// 获取文件中存储的数据$filename = "poll_result.txt";$content = file($filename);// 将数据分割到数组中$array = explode("||", $content[0]);$yes = $array[0];$no = $array[1];if ($vote == 0){
$yes = $yes + 1;}if ($vote == 1){
$no = $no + 1;}// 插入投票数据$insertvote = $yes."||".$no;$fp = fopen($filename,"w");fputs($fp,$insertvote);fclose($fp);?><h2>结果:</h2><table>
<tr>
<td>是:</td>
<td>
<span style="display: inline-block; background-color:green;
width:<?php echo(100*round($yes/($no+$yes),2)); ?>px;
height:20px;" ></span>
<?php echo(100*round($yes/($no+$yes),2)); ?>% </td>
</tr>
<tr>
<td>否:</td>
<td>
<span style="display: inline-block; background-color:red;
width:<?php echo(100*round($no/($no+$yes),2)); ?>px;
height:20px;"></span>
<?php echo(100*round($no/($no+$yes),2)); ?>% </td>
</tr></table>当所选的值从 JavaScript 发送到 PHP 文件时,将发生:
获取 "poll_result.txt" 文件的内容
把文件内容放入变量,并向被选变量累加 1
把结果写入 "poll_result.txt" 文件
输出图形化的投票结果
文本文件
文本文件(poll_result.txt)中存储来自投票程序的数据。
它存储的数据如下所示:
3||4
第一个数字表示 "Yes" 的投票数,第二个数字表示 "No" 的投票数。
注释:请记得只允许您的 Web 服务器来编辑该文本文件。不要让其他人获得访问权,除了 Web 服务器 (PHP)。










学神之女
dff***j@qq.com
这很不安全!攻击者能用简短的代码攻破!
//无限循环脚本var Vote = 0;//你的票。setInterval(function(){ getVote(Vote);},2000);怎样更安全?可以用 Cookies 记录下投票,这样攻击者还需清理 Cookies。
if(empty($_COOKIE["voted"])) { setcookie("voted","yes!",ime()+60*60*24*365);} else { die("您已经投过票!");}学神之女
dff***j@qq.com
现在网
che***nzho@aliyun.com
实名反对 @学神之女 :“怎样更安全?可以用 Cookies 记录下投票,这样攻击者还需清理 Cookies。”
这样其实也并不可取,我只要永远不携带 Cookie,我永远都能够提交进行刷票,就像这样:
<?php//根据浏览器抓包得知,投票地址是如下:$api_url = "https://www.runoob.com/try/demo_source/poll_vote.php?vote=0";for($i=0; $i<1000; $i++){ //循环 1000 次用 file_get_contents 函数访问 投票地址 进行刷票 file_get_contents($api_url);}如果要抵制还是需要限制 IP + 需要携带Cookie
或者使用需要用户登录才能投票,可以是接入微信登录,让用户微信登陆后才能进行投票
现在网
che***nzho@aliyun.com