......我认为我应该至少说出这个问题。这是一个棘手的问题。我注意到我的CAPTCHA(谁的答案恰好是一个字母,例如“A”),只要第一个字母是“A”,就会认为任何答案都是正确的。因此,如果有人要输入Apple,它会接受这个作为正确的答案。有办法解决这个问题吗?目前的代码:
}
if (!preg_match("/A/", $captcha))
{
show_error("Incorrect CAPTCHA Answer");
}
谢谢
整个.php代码:
<?php
$myemail = "email@domain.com";
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Text <domain.com>';
$captcha = $_POST['captcha'];
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail Address not valid");
}
if (!preg_match("/A/", $captcha))
{
show_error("Incorrect CAPTCHA Answer");
}
$message = "
Name: $name
E-mail: $email
Subject: $subject
Message:
$message
";
mail($myemail, $subject, $message, $from);
header('Location: page.html');
exit();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
</html>
<?php
exit();
}
?>
HTML代码的CAPTCHA方面:
<p class="c-font-lowercase">The first letter in the alphabet is?
<input type="text" placeholder="CAPTCHA Answer" name="captcha" class=""/>
</p>
答案 0 :(得分:0)
您根本不需要preg_match()
:
if ($captcha != "A") {
show_error("Incorrect CAPTCHA Answer");
}
但是,如果您 想要使用它,请确保锚定您的正则表达式:
if (!preg_match("/^A$/", $captcha)) {
show_error("Incorrect CAPTCHA Answer");
}