我有一个表单页面,用户必须在其中放入我通过电子邮件发送给他们的代码,这个表单有一个操作文件redirect.php,其中包含以下代码:
<?php
header('Location: page.php?code='.$_POST['code']);
?>
现在,我想对我的表单页面做的是显示一个错误,或者如果他们在我的表单上输入的代码在code.txt文件中不存在,则重定向到一个页面。
有可能吗?如果是怎么做的?
编辑:
我认为这一定更容易:我想要这种形式:
<form action="page.php" method="post">
<input id="code" name="code" type="text" />
<input type="submit" value="Submit"/>
</form>
只有当输入id="code"的值与code.txt
使用javascript:
<script language="JavaScript" type="text/javascript">
function checkcode ( form )
{
var pattern = /name="code" value="|code|code1|code2|"/g
if (!pattern.test(form.code.value)) {
alert( "The code is incorrect." );
form.code.focus();
return false ;
}
return true ;
}
</script>
if(!in_array($_GET['code'], file('code.txt')) {
echo "Error";
}
//or
if(strpos(file_get_contents('code.txt'), $_GET['code']) === false) {
echo "Error";
}