我在 PHP 中设置了一个考试,它采用用户名并回答四个问题。
HTML:
<html>
<head>
</head>
<body>
<form action="submit.php" method=post>
Your name: <input type=text name=yname value="" size=20 /><br><br>
Question 1:<input type=text name=q1 value="" size=20 /><br>
Question 2:<input type=text name=q2 value="" size=20 /><br>
Question 3:<input type=text name=q3 value="" size=20 /><br>
Question 4:<input type=text name=q4 value="" size=20 /><br>
<input type=submit name=btnsubmit value="submit" />
</form>
</body>
</html>
我的 PHP 示例“submit.php”:
<?php
$name = trim(strip_tags(stripslashes($_POST['yname'])));
$q1 = trim(strip_tags(stripslashes($_POST['q1'])));
$q2 = trim(strip_tags(stripslashes($_POST['q2'])));
$q3 = trim(strip_tags(stripslashes($_POST['q3'])));
$q4 = trim(strip_tags(stripslashes($_POST['q4'])));
if ($name <> "" && $q1 <> "" && $q2 <> "" && $q3 <> "" && $q4 <> "") {
if ($passing > 85) { //value checked in the more comprehensive script
if ($name == {match from file to see if exam was taken already}) {
echo "Sorry, but you took the exam already";
}
else {
//....
}
}
else {
//...
}
}
?>
我要询问的是,保存“名称”的最佳方法是什么,以便以后如果用户要重新参加考试,可以在不使用数据库的情况下进行比较:
...
if ($name == {match from file to see if exam was taken already}) {
...
如果可能的话。
更新:[已解决]
<?php
global $names;
$name = trim(strip_tags(stripslashes($_POST['yname'])));
$q1 = trim(strip_tags(stripslashes($_POST['q1'])));
$q2 = trim(strip_tags(stripslashes($_POST['q2'])));
$q3 = trim(strip_tags(stripslashes($_POST['q3'])));
$q4 = trim(strip_tags(stripslashes($_POST['q4'])));
$passing = 85;
$path_to_names_file = 'storename.txt';
if ($name <> "" && $q1 <> "" && $q2 <> "" && $q3 <> "" && $q4 <> "") {
if ($passing >= 85) { //value checked in the more comprehensive script
$names = file($path_to_names_file);
if(file_exists($path_to_names_file) && in_array($name, $names)) {
echo "Sorry, but you took the exam already"; //$name has already taken exam
}
else { //if name doesn't match, the user is taking it first time. write the name to file for future check
$fp = fopen($path_to_names_file, 'a');
fputs($fp, "\n" . $name);
fclose($fp);
//file_put_contents($path_to_names_file, "$name\n", FILE_APPEND); //append the file
echo "Your name is saved";
}
}
else {
echo "did not pass";
}
}
?>