Código PHP:
<?php
$ip = getenv(REMOTE_ADDR);
$RESULT_FILE_NAME = "poll_data.txt";
$que = "question.txt";
$ans = "ans.txt";
$fn = fopen ($que, "r");
$puff = fread ($fn, filesize($que));
fclose ($fn);
$QUESTION = "$puff";
$lis= 0;
$plsr = file("ans.txt");
for($x=0;$x<sizeof($plsr);$x++) {
$temp = explode("|",$plsr[$x]);
$list[$lis] = $temp[0];
$lis++;
}
$ANSWER = $list;
extract($HTTP_GET_VARS);
extract($HTTP_POST_VARS);
$fname="ip_log.dat";
if (strlen($answer)<=0&&!$vresult){
echo "<FORM METHOD=\"POST\">\n";
echo "<TABLE align=center border=1 borderColor=#001199 cellPadding=2 class=normaltext style=\"BORDER-BOTTOM-COLOR: #001199; BORDER-COLLAPSE: collapse; BORDER-LEFT-COLOR: #001199; BORDER-RIGHT-COLOR: #001199; BORDER-TOP-COLOR: #001199\" >\n";
echo "<TR><TH>$QUESTION</TH></TR>\n";
while (list($key, $val) = each($ANSWER)) {
echo "<TR><TD align=\"left\"><INPUT TYPE=\"radio\" NAME=\"answer\" VALUE=\"$key\"> $val</TD></TR>\n";}
echo "<TR><TD align=\"center\"><INPUT TYPE=\"Submit\" NAME=\"vote\" VALUE=\" Vote \"></TD></TR>\n";
echo "<TR><TD align=\"center\"></form><form method=post><input type=hidden name=vresult value=1><INPUT TYPE=\"Submit\" NAME=\"result\" VALUE=\"Result\">\n";
echo "</TABLE></form>";
}
if (strlen($answer)>0) {
$plsr = file("logger.txt");
for($x=0;$x<sizeof($plsr);$x++) {
$temp = explode("|",$plsr[$x]);
}
if($ip==$temp[0] && strlen($answer)>0 ){
echo "<font color=red size=1><div align=center>You have already voted</div></font>";
$file_array = file($RESULT_FILE_NAME);
if ($answer < count($ANSWER) && $vote) {
while (list($key, $val) = each($file_array)) {
$total += $val;
}
echo "<TABLE align=center border=1 borderColor=#001199 cellPadding=2 class=normaltext style=\"BORDER-BOTTOM-COLOR: #001199; BORDER-COLLAPSE: collapse; BORDER-LEFT-COLOR: #001199; BORDER-RIGHT-COLOR: #001199; BORDER-TOP-COLOR: #001199\" width=60%>\n";
echo "<tr><th>Options</th><th>Percentage</th><th>Votes</th></tr>";
while (list($key, $val) = each($ANSWER)){
$percent = $file_array[$key] * 100 / $total;
$percent_int = floor($percent);
$percent_float = number_format($percent, 1);
$tp += $percent_float;
if($percent_int>=75){
$color="blue";
}
elseif($percent_int>=50){
$color="green";
}
elseif($percent_int>=25){
$color="orange";
}
elseif($percent_int<25){
$color="red";
}
echo "<tr><td> $ANSWER[$key] </td><td><table cellpadding=1 cellspacing=0 width=100% border=0 bgcolor=black><tr><td><table cellpadding=0 cellspacing=0 border=0 width=100%>
<tr>
<td bgcolor=$color width=$percent_int% height=10 style=border:0 >
<spacer type=block width=2 height=8>
</td>
<td bgcolor=white width=91% height=10 style=border:0 >
<spacer type=block width=2 height=8>
</td>
</tr>
</table>
</td>
</tr>
</table>$percent_float%</td><td>$file_array[$key]</td></tr>";
}
$tv=$total;
echo "<td>Total Votes: $tv</td></TABLE>";
}
}elseif (strlen($answer)>0 && $ip!=$temp[0]){
$file_array = file($RESULT_FILE_NAME);
if ($answer < count($ANSWER) && $vote) {
$old_answer = $file_array[$answer];
$old_answer = preg_replace("/\n\r*/", "", $old_answer);
$file_array[$answer] = ($old_answer + 1)."\n";
$fname="logger.txt";
$fq = fopen($fname, "a++");
fwrite ($fq, $ip);
fwrite ($fq, "|");
fwrite ($fq, "\n");
fclose ($fq);
$file = join('', $file_array);
$fp = fopen("$RESULT_FILE_NAME", "w");
flock($fp, 1);
fputs($fp, $file);
flock($fp, 3);
fclose($fp);
echo "<div align=center>Vote saved </div>";
}
while (list($key, $val) = each($file_array)) {
$total += $val;
}
echo "<TABLE align=center border=1 borderColor=#001199 cellPadding=2 class=normaltext style=\"BORDER-BOTTOM-COLOR: #001199; BORDER-COLLAPSE: collapse; BORDER-LEFT-COLOR: #001199; BORDER-RIGHT-COLOR: #001199; BORDER-TOP-COLOR: #001199\" width=60%>\n";
echo "<tr><th>Options</th><th>Percentage</th><th>Votes</th></tr>";
while (list($key, $val) = each($ANSWER)){
$percent = $file_array[$key] * 100 / $total;
$percent_int = floor($percent);
$percent_float = number_format($percent, 1);
$tp += $percent_float;
if($percent_int>=75){
$color="blue";
}
elseif($percent_int>=50){
$color="green";
}
elseif($percent_int>=25){
$color="orange";
}
elseif($percent_int<25){
$color="red";
}
echo "<tr><td> $ANSWER[$key] </td><td><table cellpadding=1 cellspacing=0 width=100% border=0 bgcolor=black><tr><td><table cellpadding=0 cellspacing=0 border=0 width=100%>
<tr>
<td bgcolor=$color width=$percent_int% height=10 style=border:0>
<spacer type=block width=2 height=8>
</td>
<td bgcolor=white width=91% height=10 style=border:0>
<spacer type=block width=2 height=8>
</td>
</tr>
</table>
</td>
</tr>
</table>$percent_float%</td><td>$file_array[$key]</td></tr>";
}
$tv=$total;
echo "<td>Total Votes: $tv</td></TABLE>";
}
else {
echo "Please Select an option first";
}
}
if (!empty ($vresult)){
$file_array = file($RESULT_FILE_NAME);
while (list($key, $val) = each($file_array)) {
$total += $val;
}
echo "<TABLE align=center border=1 borderColor=#001199 cellPadding=2 class=normaltext style=\"BORDER-BOTTOM-COLOR: #001199; BORDER-COLLAPSE: collapse; BORDER-LEFT-COLOR: #001199; BORDER-RIGHT-COLOR: #001199; BORDER-TOP-COLOR: #001199\" width=60%>\n";
echo "<tr><th>Options</th><th>Percentage</th><th>Votes</th></tr>";
while (list($key, $val) = each($ANSWER)){
$percent = $file_array[$key] * 100 / $total;
$percent_int = floor($percent);
$percent_float = number_format($percent, 1);
$tp += $percent_float;
if($percent_int>=75){
$color="blue";
}
elseif($percent_int>=50){
$color="green";
}
elseif($percent_int>=25){
$color="orange";
}
elseif($percent_int<25){
$color="red";
}
echo "<tr><td> $ANSWER[$key] </td><td><table cellpadding=1 cellspacing=0 width=100% border=0 bgcolor=black><tr><td><table cellpadding=0 cellspacing=0 border=0 width=100%>
<tr>
<td bgcolor=$color width=$percent_int% height=10 style=border:0 >
<spacer type=block width=2 height=8>
</td>
<td bgcolor=white width=91% height=10 style=border:0 >
<spacer type=block width=2 height=8>
</td>
</tr>
</table>
</td>
</tr>
</table>$percent_float%</td><td>$file_array[$key]</td></tr>";
}
$tv=$total;
echo "<td>Total Votes: $tv</td></TABLE>";
}
?>
Un saludo Papirpo, y recordad que cuando digo que soy novato me refiero a que soy muy novato