我发送了一个表单,打开另一个php文件。我想获取表单信息,并创建一个表,但如果有人写了表中的一些单词,代码应该回应"抱歉"。代码的第一部分正在工作,它获取表单,并在index.php中创建一个表,但是当我创建相同的表单时,它再次发送:S我如何更改它?
<?
$original_list = file_get_contents("index.php");
$file = fopen("index.php","w") or exit("Unable to open file!");
$since = $_POST["since"];
$since2 = "<tr><td class=\"since\"> $since </td><br/>";
$user = $_POST["user"];
$user2 = "<td class=\"content\"> $user </td><br/>";
$due = $_POST["due"];
$due2 = "<td class=\"due\"> $due </td></tr>\n";
if (strpos("index.php","word") === true) {
echo "Sorry"
}elseif ($_POST["since"] <> "");{
fwrite($file,"$since2$user2$due2$original_list");
}
fclose($file);
?>
答案 0 :(得分:2)
更改此行:
if (strpos("index.php","word") === true) {
到
if (strpos($original_list,"word") !== false) {
strpos()返回值
返回针相对于针的位置 haystack字符串的开头(与offset无关)。另请注意 字符串位置从0开始,而不是1.
如果未找到针,则返回FALSE。
答案 1 :(得分:0)
尝试这样做,因为你没有查看文件内容:
<?
$original_list = file_get_contents("index.php");
$file = fopen("index.php","w") or exit("Unable to open file!");
$since = $_POST["since"];
$since2 = "<tr><td class=\"since\"> $since </td><br/>";
$user = $_POST["user"];
$user2 = "<td class=\"content\"> $user </td><br/>";
$due = $_POST["due"];
$due2 = "<td class=\"due\"> $due </td></tr>\n";
if (strpos($original_list,"word") ) { // <- this is the changed line
echo "Sorry"
}elseif ($_POST["since"] <> "");{
fwrite($file,"$since2$user2$due2$original_list");
}
fclose($file);
?>