0

誰かが私を助けて正しい道に導いてくれませんか

シンプルなフォームとバリデーションがありますが、メールに常にスパムが届くため、ロボットがスパム メッセージを送信するのを防ぐために、CAPTCHA を入力することにしました。

問題は、キャプチャが空白であっても、メッセージがまだ私のメールに送信されていることです。問題が何であるかを指摘するのは難しいです。私はまだ PHP の学習過程にあるためです。要するに、私は PHP の基本的な知識しか持っていません。

ところで、これは私のコードです:

HTML :

<form method="post" action="#">
    <div class="inputFields">
    <!-- <legend>Comment Form</legend> -->
        <p>
            <label for="name">Name</label><br />
            <input type="text" name="name" id="name"  <?php if(isset($name)){ echo 'value="' . $name . '" />';} else { echo 'value="" />';} ?>
        </p>
        <p>
            <label for="email">Email </label><br />
            <input type="text" name="email" id="email" <?php if(isset($email)){ echo 'value="' . $email . '" />';} else { echo 'value="" />';} ?>
        </p>

        <p>
            <label for="location">Location</label><br />
            <input type="text" name="location" id="location" <?php if(isset($location)){ echo 'value="' . $location . '" />';} else { echo 'value="" />';} ?>
        </p>

        <p>
            <label class="yourMessage" for="comment">Your message</label><br />
            <textarea cols="30" rows="10" name="message" id="comment"><?php if(isset($message)){ echo $message;}?></textarea>
        </p>


        <p>
            <label class="captcha" for="captcha">Enter text from image</label><br />
            <img src="test/seccode.php" width="100" height="36" alt="" /><br /><br />
            <input type="text" name="captcha" value="" id="captcha"/>
        </p>



    </div>
    <p>
        <input class="submit" type="submit" name="submit" value="Submit" />
    </p>
</form>

検証

<?php
$submit = htmlentities($_POST['submit']);
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
$message = htmlentities($_POST['message']);

if($submit == 'Submit') {

    ###### Validate 'name' and combat Magic Quotes if necessary

    if(isset($name) && !empty($name)) {
        $name2 = stripslashes($name);
        $nameCheck = strlen($name2);
    } else {
        $err_name = '<span>Please enter your <strong>name</strong>.</span>';
        $isError=true;
    }

    ### check if data has numbers

    if(is_numeric($name) ) {
        $err_name = '<span>Please enter a valid <strong>name</strong>. No numbers please.</span>';
        $isError=true;
    }

    if($_POST['captcha'] != $_SESSION['secCode']) {
        // wrong security code
        $err_captcha = '<span><strong>WRONG CODE!</strong></span>';
        $isError=true;
    }
    else {
        // security code is valid; reset it!
        $_SESSION['secCode'] = rand(100000, 999999);
    }


    ###### Validate 'email' and combat Magic Quotes if necessary

    if(!empty($email) && eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
    } else {
        $err_email = '<span>Please enter a valid <strong>email address</strong>.</span>';
        $isError=true;
    }

    ###### Validate 'message' and combat Magic Quotes if necessary

    if(!empty($message)) {
        $message2 = stripslashes($message);
        $messageCheck = strlen($message2);
    } else {
        $err_message = '<span>Please enter your <strong>message</strong>.</span>';
        $isError=true;
    }


    ###### Mailing Address

    $to  = 'myemail@gmail.com';

    $headers="From: $email\n";

    ###### Body of the email to be sent:

    $message1="Name: $name\nEmail: $email\n\nMessage: $message";

    //$message2="$name \'s message:\n\n\"$message\"";


    ###### Mailing the data

    //!!!!!!!!!!! UNCOMMENT MAIL BELOW WHEN UPLOADED TO WEB HOST !!!!!!!!!!!

    if(!isset($isError)) {

        if(!empty($message)) {
            mail($to,"Message from www.myemail.com",$message1,$headers);
        } else {
            //echo 'fail';
            $err_message = '<font color="red">Please enter your message.</font>';
            $isError=true;
        }
    }


    ###### Forwarding to another page

    if(!empty($isError)) {
    } else {
        header("Location: thank_you.php");
    }
}



?>
4

2 に答える 2