douwen9540
2017-02-09 12:13
浏览 149
已采纳

我在google验证验证码中收到邮件设置错误

<!DOCTYPE html>
<html>
    <head>
        <title>
            Google Captcha Form With Send Mail
        </title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <form id="contactform" action="action.php" method="post" class="form" role="form">
            <div class="form-group">
                <div class="row">
                    <div class="col-lg-3">
                        <label>Name:</label>
                    </div>
                    <div class="col-lg-5">
                        <input class="form-control required" id="vname" name="vname" placeholder="Your Name" type="text" required />
                    </div>
                </div>
            </div>
            <br>
            <div class="form-group">
                <div class="row">
                    <div class="col-lg-3">
                        <label>Email:</label>
                    </div>
                    <div class="col-lg-5">
                        <input class="form-control required" id="vemail" name="vemail" placeholder="Your Email" type="email" required />
                    </div>
                </div>
            </div>
            <br>
            <div class="form-group">
                <div class="row">
                    <div class="col-lg-3">
                        <label>Information:</label>
                    </div>
                    <div class="col-lg-5">
                        <textarea type="text" name="msg" id="msg" rows="5" cols="50" class="form-control"></textarea>
                    </div>
                </div>
            </div>
            <br>
            <div class="form-group">
                <div class="row">
                    <div class="col-lg-3">
                        <label>Verify Whether Human</label>
                    </div>
                    <div class="col-lg-5">
                        <div class="g-recaptcha" data-sitekey="PRIVATE" class="form-control">
                        </div>
                    </div>
                </div>
            </div>
            <br>
            <div class="form-group">
                <input type="submit" class="btn btn-success form-send" value="Send">
            </div>
        </form>
        <script src="js/jquery.min.js"></script>
        <script src="js/recaptcha.js"></script>
        <script src="js/bootstrap.min.js"></script>
    </body>
</html>

and here is the following php

<?php
$captcha;
if (isset($_POST['g-recaptcha-response'])) { $captcha = $_POST['g-recaptcha-response']; }
// Check for correct reCAPTCHA
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret='PRIVATE'&response=" . $captcha . '&remoteip=' . $_SERVER['REMOTE_ADDR']);
if (!$captcha || $response.success == false) {
    echo "Your CAPTCHA response was wrong";
    exit ;
} else {
    // Check for Blank Fields..
    if ($_POST["vname"] == "" || $_POST["vemail"] == "" || $_POST["msg"] == "") {
        echo "Please fill all required fields";
    } else {
        // Check if the "Sender's Email" input field is filled out
        $email = $_POST['vemail'];
        // Sanitize E-mail Address
        $email = filter_var($email, FILTER_SANITIZE_EMAIL);
        // Validate E-mail Address
        $email = filter_var($email, FILTER_VALIDATE_EMAIL);
        if (!$email) {
            echo "Invalid Sender's Email";
        } else {
            $to = 'jinam@evoplus.in';
            $subject = 'New Form Entry';
            $message = "New message was submitted from <br /> " . "<strong>" . $_POST['vname'] . "</strong>" . "<br /><br />The message is:<br />" . "<strong>" . $_POST['msg'] . "</strong>";
            $headers = "From:" . $_POST['vname'] . "<" . $email . ">";
            $headers .= "MIME-Version: 1.0" . "
";
            $headers .= "Content-type:text/html;charset=UTF-8" . "
";
            // Sender's Email
            // Message lines should not exceed 70 characters (PHP rule), so wrap it
            $message = wordwrap($message, 70, "
");
            // Send Mail By PHP Mail Function
            if (mail($to, $subject, $message, $headers)) {
                echo "Your mail has been sent successfully!";
            } else {
                echo "Failed to send email, try again.";
                exit ;
            }
        }
    }
}
?>

Notice: Use of undefined constant success - assumed 'success' in C:\xampp\htdocs\captchaform\action.php on line 6

i am getting this error help me with this i triend decoding with json no help so where am i wrong

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanke1984 2017-02-09 12:28
    已采纳

    use json_decode()

    $response = json_decode($response);
    

    and check like this

    if (!$captcha || $response->success == false) {
    

    To see more in deep : http://php.net/manual/en/function.json-decode.php

    点赞 打赏 评论
  • doumao8355 2017-02-09 12:27

    $response is having JSON data and PHP you can not use . operator to fetch its keys. - Convert to array - access the keys

    as your response is something like this

    { "success": true, "challenge_ts": "2017-02-09T12:18:22Z", "hostname": "localhost" }
    

    Do something like this:

    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret='PRIVATE'&response=" . $captcha . '&remoteip=' . $_SERVER['REMOTE_ADDR']);
    // Convert json to array
    $response = ($response) ? json_decode($response, true) : [];
    // If array not empty then perform your logic
    if ($res) {
       if (!$captcha || $response['success'] == false) {
        echo "Your CAPTCHA response was wrong";
        exit ; 
      } else { 
       // your code 
         .....
          ....
      }
    }
    

    This will work.

    点赞 打赏 评论

相关推荐 更多相似问题