我正在尝试创建“与我们联系”表单。
我的CAPTCHA验证任何随机输入,我不知道为什么......我的比较器不起作用。其他静态验证有效,例如名字,姓氏和电子邮件。 php验证发生在服务器端
请帮忙......
在此先感谢Mike C A
的index.php
<?php
session_start();
$string = $_SESSION["random_code"];
$firstname = $lastname = $telephone = $email = $subject = $code = $message = "";
$errfirstname = $errlastname = $errtelephone = $erremail = $errsubject = $errcode = $errmessage = "";
function test_input($data) {
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = strip_tags($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$firstname = test_input($_POST["firstname"]);
if (empty(test_input($_POST["firstname"]))) {
$errfirstname = "First name is required";
} else {
$firstname = test_input($_POST["firstname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
$errfirstname = "Only letters and white space allowed";
}
}
$lastname = test_input($_POST["lastname"]);
if (empty(test_input($_POST["lastname"]))) {
$errlastname = "Last name is required";
} else {
$lastname = test_input($_POST["lastname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
$errlastname = "Only letters and white space allowed";
}
}
$telephone = test_input($_POST["telephone"]);
if (empty(test_input($_POST["telephone"]))) {
$errtelephone = "";
} else {
$telephone = test_input($_POST["telephone"]);
if (!preg_match("/^\+[0-9]{7,18}$|^[0-9]{7,19}$/",$telephone)) {
$errtelephone = "Only numbers and + signs allowed";
}
}
$email = test_input($_POST["email"]);
if (empty(test_input($_POST["email"]))) {
$erremail = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
$erremail = "Invalid email format";
}
}
$subject = test_input($_POST["subject"]);
if (empty(test_input($_POST["subject"]))) {
$errsubject = "";
} else {
$subject = test_input($_POST["subject"]);
if (!preg_match("/[a-z|A-Z|0-9|\s]*/",$subject)) {
$errsubject = "Up to 20 letters, numbers, and whitespace allowed";
}
if (strlen($subject) > 20) {
$errsubject = "Up to 20 letters, numbers, and whitespace allowed";
}
}
$message = test_input($_POST["message"]);
if (empty(test_input($_POST["message"]))) {
$errmessage = "A message is required";
} else {
$message = test_input($_POST["message"]);
if (!strlen($message) > 15) {
$errmessage = "At least 15 letters, numbers, and whitespace required";
}
}
$code = test_input($_POST["code"]);
if (empty(test_input($_POST["code"]))) {
$errcode = "Verify that you're human";
} else {
$code = test_input($_POST["code"]);
if (!test_input($_POST['code']) == $string) {
$errcode = "Match the scrambled code";
}
}
if(isset($_POST['submit'])) {
if($errfirstname == "" && $errlastname == "" && $errtelephone == "" && $erremail == "" && $errsubject == "" && $errcode == "" && $errmessage == "") {
$email_from = 'mikethebos@icloud.com';
$email_body = "Message from: $firstname $lastname\r\nPhone number: $telephone\r\nE-mail address: $email\r\nSubject: $subject\r\nMessage: $message";
$to = $email_from;
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$subject,$email_body,$headers);
echo "SENT";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>The Bash Catalog - Contact Us</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
<p><label for="firstname">First Name: </label><input type="text" size="10" name="firstname" id="firstname" value="<?= isset($_POST['firstname']) ? $_POST['firstname'] : ''; ?>" placeholder="John" /><span class="error"> * <?php echo $errfirstname;?></span></p>
<p><label for="lastname">Last Name: </label><input type="text" name="lastname" size="15" id="lastname" value="<?= isset($_POST['lastname']) ? $_POST['lastname'] : ''; ?>" placeholder="Smith" /><span class="error"> * <?php echo $errlastname;?></span></p>
<p><label for="telephone">Phone number (+country): </label><input type="text" name="telephone" id="telephone" value="<?= isset($_POST['telephone']) ? $_POST['telephone'] : ''; ?>" placeholder="+10123456789" minlength="7" maxlength="19" /><span class="error"> <?php echo $errtelephone;?></span></p>
<p><label for="email">E-mail address: </label><input type="email" name="email" id="email" size="25" value="<?= isset($_POST['email']) ? $_POST['email'] : ''; ?>" placeholder="JohnSmith@example.com" /><span class="error"> * <?php echo $erremail;?></span></p>
<p><label for="subject">Subject: </label><input type="text" name="subject" id="subject" size="23" value="<?= isset($_POST['subject']) ? $_POST['subject'] : ''; ?>" placeholder="New Linux Distrobution" /><span class="error"> <?php echo $errsubject;?></span></p>
<p class="message"><label class="message" for="message">Message: </label><textarea name="message" class="message" id="message" cols="60" rows="20" placeholder="Type you message or comments"><?php if(isset($_POST['message'])) { echo htmlentities ($_POST['message']); }?></textarea><span class="error"> * <?php echo $errmessage;?></span></p>
<img src="captcha.php"/>
<p><label for="code">Scrambled code: </label><input type="text" name="code" id="code" /><span class="error"> * <?php echo $errcode;?></span></p>
<p><input type="submit" name="submit" value="Send" /></p>
</form>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
captcha.php
<?php
session_start();
$string = '';
for ($i = 0; $i < 10; $i++) {
// $lower = chr(rand(97, 122));
// $upper = chr(rand(65, 90));
// $rcomb = array($lower, $upper);
// $srand = array_rand($rcomb, 1);
// $ssrand = (string)$srand;
// $string .= $ssrand;
$int = rand(0,25);
$a_z = "abcdefghijklmnopqrstuvwxyz";
$rand_letter = $a_z[$int];
$string .= $rand_letter;
}
$_SESSION["random_code"] = $string;
$dir = 'font/';
$dirFonts = glob($dir . '*.{ttf}', GLOB_BRACE);
$randomFont = $dirFonts[array_rand($dirFonts)];
$fontBox = imagettfbbox(70, 0, $randomFont, $_SESSION["random_code"]);
$fontx = $fontBox[2] + 10;
$fonty = $fontBox[3] + 10;
$image = imagecreatetruecolor($fontx + 25, $fonty + 70); // size of image wxh
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $fontx + 30, $fonty + 70, $white);
for ($i = 0; $i < 5; $i++) {
$rr = rand(0, 255);
$rg = rand(0, 255);
$rb = rand(0, 255);
$colorr = imagecolorallocate($image, $rr, $rg, $rb);
$poly_val = array();
$randomn = rand(3, 20);
for ($i = 0; $i < $randomn; $i++){
$rx = rand(0, $fontx + 30);
$ry = rand(0, $fonty + 60);
array_push($poly_val, $rx, $ry);
}
ImageFilledPolygon($image, $poly_val, $randomn, $colorr);
}
imagettftext($image, 70, 0, 5, $fonty + 65, $black, $randomFont, $_SESSION["random_code"]);
for ($x = 0; $x < 1500; ++$x) {
$fontty = $fonty / 2;
$amplitude = rand(0, $fontty + 70);
$randcoe = rand(0, 180);
$y = 20 + $amplitude * sin(deg2rad($x-$randcoe));
$rrr = rand(0, 255);
$rrg = rand(0, 255);
$rrb = rand(0, 255);
$colorrr = imagecolorallocate($image, $rrr, $rrg, $rrb);
imagesetpixel($image, $x, $y, $colorrr);
$ax = $x;
imagesetpixel($image, $ax++, $y++, $colorrr);
$rrr = rand(0, 255);
$rrg = rand(0, 255);
$rrb = rand(0, 255);
$colorrr = imagecolorallocate($image, $rrr, $rrg, $rrb);
imagesetpixel($image, $ax++, $y++, $colorrr);
$rrr = rand(0, 255);
$rrg = rand(0, 255);
$rrb = rand(0, 255);
$colorrr = imagecolorallocate($image, $rrr, $rrg, $rrb);
imagesetpixel($image, $ax++, $y++, $colorrr);
$rrr = rand(0, 255);
$rrg = rand(0, 255);
$rrb = rand(0, 255);
$colorrr = imagecolorallocate($image, $rrr, $rrg, $rrb);
imagesetpixel($image, $ax++, $y++, $colorrr);
$rrr = rand(0, 255);
$rrg = rand(0, 255);
$rrb = rand(0, 255);
$colorrr = imagecolorallocate($image, $rrr, $rrg, $rrb);
imagesetpixel($image, $ax++, $y++, $colorrr);
$rrr = rand(0, 255);
$rrg = rand(0, 255);
$rrb = rand(0, 255);
$colorrr = imagecolorallocate($image, $rrr, $rrg, $rrb);
imagesetpixel($image, $ax++, $y++, $colorrr);
$rrr = rand(0, 255);
$rrg = rand(0, 255);
$rrb = rand(0, 255);
$colorrr = imagecolorallocate($image, $rrr, $rrg, $rrb);
imagesetpixel($image, $ax++, $y++, $colorrr);
$rrr = rand(0, 255);
$rrg = rand(0, 255);
$rrb = rand(0, 255);
$colorrr = imagecolorallocate($image, $rrr, $rrg, $rrb);
}
imageline($image, 0, 0, 420, 120, $black);
imageline($image, 420, 0, 0, 120, $color);
imageline($image, 210, 0, 210, 120, $black);
header("Content-type: image/png");
imagepng($image);
?>