【发布时间】:2012-05-23 03:23:05
【问题描述】:
我正在尝试使用平面文件创建一个评分系统(我知道,我也更喜欢使用 mysql)。我已经设法创建了足够多的变量,以便将变量从游戏中提取出来(用户名和分数)并放入所述文本文件中。我还可以在格式化表格中输出这个文本文件。但是我无法让分数按任何顺序排序 - 理想情况下我想要降序。在网上搜索后,我能找到的最好的方法是在附加的代码中,但是目前这似乎并没有真正做任何事情。我知道这可能是一个愚蠢的错误,但我可以用一双新的眼睛来看看它。 非常感谢,哈利
<?php
echo '<table width="5%" border="5"><tr><th>Your Score</th><th>Your Name</th></tr>';
echo '<tr><td>'.$_POST["m_score"].'</td><td>'.$_POST["name"].'</td></tr>';
echo '</table>';
echo "<br />";
$data=$_POST["m_score"].",".$_POST["name"].",";
$filename = "./highscores.txt";
$handle = fopen($filename, "a");
if (!$handle)
{
print "There were problems opening the file";
exit();
}
fwrite($handle, $data);
fclose($handle);
$handle = fopen($filename, "r");
$datain = fread($handle, filesize($filename));
$value = explode(",",$datain);
$row = floor(count($value)/2);
$count = 0;
$filename = file("./highscores.txt");
sort($filename);
file_put_contents("./highscores.txt", implode($filename));
echo '<table width="5%" border="1"><tr><th>Top Scores</th><th>Top Users</th></tr>';
for ($i=0;$i<$row;$i++)
{
echo '<tr>';
for($x=0;$x<2;$x++){
echo "<td>$value[$count]</td>";
$count++;
}
echo "</tr>";
}
echo '</table>';
fclose($handle);
?>
【问题讨论】:
-
你的代码对 XSS 开放 + 你使用的 implode 只需要 1 个参数,它需要 2 个,加上通知未定义的变量,启用
error_reporting(E_ALL)!!!你为什么不使用数据库,甚至只使用 sqlite? -
Webbiedave,我复制的唯一代码是我从这个站点获得的 sort($filename) 段,但对这种态度表示赞赏!真的让我觉得值得注册。 Lawrence,XSS 在这段代码中并不是一个大问题,它意味着一个非常低级别的工作(我相信你已经从我糟糕的尝试中猜到了)。我很乐意使用替代数据库,实际上(令人讨厌)最初是使用 mysql 编写的。但是,它必须使用平面文件方法编写——这对我来说是第一次。