我写了一个显示随机方程的php函数。如果未提供正确答案,则提交失败。方程是这样的:
[随机数字或文本数字] [随机选择+,-,x] [随机数字或文本数字]= ?
所以你可能会看到:8+2=?,或 7 - 4 = ?,等等
我手动测试了它,它似乎工作得很好。我无法正确回答 CAPTCHA。尽管有此验证码,但某些人或某人仍在发布垃圾邮件。 所以我的问题是,有没有可以解决这个问题的程序?
这是我的表格:
<form action="scripts/handler_post.php" method="post" id="gb_form">
<h3>Leave us a comment:</h3><br />
<div id="form_msg"><?php echo $msg; ?></div>
<input type="text" name="name" value="Your Name" />
<textarea name="message">Your Message</textarea>
<?php include('includes/bmw_captcha.php'); ?>
<div style="color:red; font-size:90%;">*What does <?php echo $array_num1[1] . " " . $array_calculation[0] . " " . $array_num2[1] . " = "; ?>
<input type='text' maxlength='2' name='captcha' style="color:#640513; width:25px;" />?</div>
<div><span style="font:italic 90% Arial; color:#666;">(For security purposes, please answer this CAPTCHA problem.)</span></div>
<input type="hidden" name="captcha_answer" value="<?php echo $array_calculation[1]; ?>" />
<input type="submit" name="submit_button" value="Post" />
</form>
这是处理表单的脚本:
// submitted values
$name = clean_string( $_POST['name'] );
$message = clean_string( $_POST['message'] );
$captcha = clean_string( $_POST['captcha'] );
$captcha_ans= clean_string( $_POST['captcha_answer'] );
// check if name var is empty or contains digits
if( empty($name) || (strcspn( $name, '0123456789' ) != strlen( $name )) )
{ header( 'Location: /guestbook.php?msg=n' ); }
else if( empty($message) )
{ header( 'Location: /guestbook.php?msg=m' ); }
else if( empty($captcha) || $captcha != $captcha_ans )
{ header( 'Location: /guestbook.php?msg=cap' ); }
else
{
$query = "
INSERT INTO $table_posts ( id, name, message, timestamp )
VALUES( 0, \"$name\", \"$message\", now() )
";
$result = $db_connect->query( $query ); // execute the query
if( $result )
{
header('Location: ../guestbook.php?msg=1');
}
else
header('Location: ../guestbook.php?msg=2');
}