-1

Web サイトのユーザー向けに Ajax 投票を行っていますが、私たちの場合、ユーザーはページを更新した後、無制限に投票でき、結果が記録されています。

ユーザーが自分の IP から 1 回だけ投票できるようにし、投票した後、ページを更新して結果を表示できるようにしたいと考えています。

ここにコードがあります

HTML ページ:

<html>
<head>
<script>
function getVote(int)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("poll").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="poll">
<h3>Do you like PHP and AJAX so far?</h3>
<form>
Yes:
<input type="radio" name="vote" value="0" onclick="getVote(this.value)">
<br>No:
<input type="radio" name="vote" value="1" onclick="getVote(this.value)">
</form>
</div>

</body>
</html> 

PHP ファイル:

<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0)
  {
  $yes = $yes + 1;
  }
if ($vote == 1)
  {
  $no = $no + 1;
  }

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table> 

ユーザーが自分の IP から 1 回だけ投票できるようにし、投票した後、ページを更新して結果を表示できるようにしたいと考えています。

4

1 に答える 1

0

次のようにユーザーの IP アドレスを保存できます。

$insertvote = $yes . "||" . $no . "||" . $_SERVER['REMOTE_ADDR'];

ただし、データベースがないと、ファイルが大きくなり、IP が既に存在するかどうかを確認するためにファイルをスキャンする必要があるため、管理が困難になります。セッションごとに投票を制限することもできますが、それも完全にばかげているわけではありません (ちなみに、IP ごとに投票を制限することもありません)。

<?php
session_start();

if(isset($_SESSION['voted'])){
    die('You have already voted.');
}

$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0){
    $yes = $yes + 1;
}
if ($vote == 1){
    $no = $no + 1;
}

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
$write = fputs($fp,$insertvote);
fclose($fp);

if($write !== false){
    $_SESSION['voted'] = true;
}
?>
<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table> 
于 2013-11-05T11:56:26.327 に答える