0

Web サイトに、フォーム データを外部の php ファイル (contact-form-handler.php) に送信する電子メール フォームがあります。最近、キャプチャを追加しようとしましたが、外部の php ファイルを取得して、キャプチャコードは正しく入力されました..現在、正しいコードを入力しても正しくないと表示されます。ウェブサイトはbathroomdesignperth.com.auです

フォームコード:

<?php
    $_SESSION['code'] = sha1('Same text as in the image');
?>
<form method="POST" name="contact_form" action="/templates/onlinespark/contact-form-handler.php"> 
    <label for='name'>Name: </label>
    <input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
    <label for='email'>Email: </label>
    <input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
    <label for='phone'>Phone: </label>
    <input type="text" name="phone" value='<?php echo htmlentities($phone) ?>'>
    <label for='message'>Message:</label>
    <textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
    <label><img src="/templates/onlinespark/captcha.php"></label>
    <input type="text" name="code"> 
    <input type="submit" value="Submit" name='submit' class="quoteButton">
</form>

PHP コード:

<?php 
        if (isset($_POST['submit'])) {
        $error = "";

        if (!empty($_POST['name'])) {
        $name = $_POST['name'];
        } else {
        $error .= "You didn't type in your name. <br />";
        }

                if (!empty($_POST['phone'])) {
        $name = $_POST['phone'];
        } else {
        $error .= "You didn't enter your phone. <br />";
        }

        if (!empty($_POST['email'])) {
        $email = $_POST['email'];
          if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){ 
          $error .= "The e-mail address you entered is not valid. <br/>";
          }
        } else {
        $error .= "You didn't type in an e-mail address. <br />";
        }

        if (!empty($_POST['message'])) {
        $message = $_POST['message'];
        } else {
        $error .= "You didn't type in a message. <br />";
        }

        if(sha1($_POST['code']) == $_SESSION['code']) { 
    $code = $_POST['code'];
} else { 
    $error .= "The captcha code you entered does not match. Please try again. <br />";    
}

        if (empty($error)) {
        $from = 'From: ' . $name . ' <' . $email . '>';
        $to = "mail@email.com.au";
        $subject = "New contact form message";
        $content = $name . " has sent you a message: \n" . $message;
        $success = "<h3>Thank you! Your message has been sent!</h3>";
        mail($to,$subject,$content,$from);
        }
        }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>ERROR - Please fill in all fields!</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<h1>ERROR - Please go back and fill in all fields!</h1>
<?php
            if (!empty($error)) {
            echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) returned:</strong><br/>' . $error . '</p>';
            } elseif (!empty($success)) {
            echo $success;
            }
        ?>
</body>
</html>
4

1 に答える 1

1

POST を介して外部の php ファイルにエンコードされた形式でキャプチャへの正しい答えを送信します。

<?php $salt = 'some-random-text'; ?>

<input type="text" name="code" /> 
<input type="hidden" name="code_key" value="<?php echo sha1('Text in the image' . $salt); ?>" /> 

PHP コードでは、セッション値を使用する代わりに、投稿された「code_key」を確認します。

$salt = 'some-random-text'; // same salt string as in the original file
if ($_POST['code_key'] == sha1($_POST['code'] . $salt)) { 
    // captcha is correct
} else { 
    // captcha is wrong
}

これは、異なるドメイン間でのキャプチャ チェックに最適です。$salt パラメータはセキュリティを強化するためのものであることに注意してください。

于 2013-06-12T03:52:51.997 に答える