0

Столкнулся с проблемой и неделю пытаюсь с ней наконец разобраться. На моём сайте присутствует система голосования, состоящая из 3 файлов:

poll.php:

<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>Вопросы?</h3> <form> Да <input type="radio" name="vote" value="0" onclick="getVote(this.value)"> <br> Нет <input type="radio" name="vote" value="1" onclick="getVote(this.value)"> </form> </div>

</body> </html>

poll_vote.php:

<?php
session_start();

if(isset($_SESSION['voted'])){ $filename = "poll_result.txt"; $content = file($filename);

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

$yespercentage = 100round($yes/($no+$yes),2); $nopercentage = 100round($no/($no+$yes),2);

echo &quot;&lt;style&gt;
    @media only screen and (max-width: 1080px) {
        td {
            font-size: .4em !important; 
        }    
    }
&lt;/style&gt;
&lt;h2&gt;Результаты:&lt;/h2&gt;

<table> <tr> <td>Да:</td> <td> <img src=&quot;poll.gif&quot; width='$yespercentage' height='20'> $yespercentage% </td> </tr> <tr> <td>Нет:</td> <td> <img src=&quot;poll.gif&quot; width='$nopercentage' height='20'> $nopercentage% </td> </tr> </table>"; die("Вы уже проголосовали."); }

$vote = $_REQUEST['vote'];

$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; }

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

if($write !== false){ $_SESSION['voted'] = true; } ?> <style> @media only screen and (max-width: 1080px) { table * { font-size: .4em !important; }
} </style> <h2>Результаты:</h2> <table> <tr> <td>Да:</td> <td> <img src="poll.gif" width='<?php echo(100round($yes/($no+$yes),2)); ?>' height='20'> <?php echo(100round($yes/($no+$yes),2)); ?>% </td> </tr> <tr> <td>Нет:</td> <td> <img src="poll.gif" width='<?php echo(100round($no/($no+$yes),2)); ?>' height='20'> <?php echo(100round($no/($no+$yes),2)); ?>% </td> </tr> </table>

и poll_result.txt, который выглядит примерно так:

15||5

Иногда, пытаясь голосовать, ничего не происходит и в консоли отображается ошибка:

Failed to load resource: the server responded with a status of 500 ()

Через некоторое время, зайдя на сайт, я с удивлением обнаруживаю, что код исправно работает, причём никаких изменений в нём сделано не было. Возможно, что проблема с хостом, но нет никаких сведений об ошибках.

Буду добр, если поможете!

0 Answers0