我在使用 PHP 时遇到“Google Recaptcha V2”问题

问题描述 投票:0回答:2

我的“Google Recaptcha V2”有问题,因为它总是无效的验证码,请重试。我有以下代码。

这是“API”问题吗?或问题代码?还是服务器有问题?

我的网络测试:https://nexteknologi.com/test/

enter image description here - enter image description here

<html>
<head>
    <title> Contact Form Design </title>
    <link rel="stylesheet" href="style.css">
    <script src='https://www.google.com/recaptcha/api.js'></script>
</head>
    <style>
        
    </style>
<body>
    
<div class="contact-form">
<h2>CONTACT US</h2>
<form method="post" action="">
    <input type="text" name="name" placeholder="Your Name" required>
    <input type="number" name="phone" placeholder="Phone No">
    <input type="email" name="email" placeholder="Email Address" required>
    <textarea name="message" placeholder="Your Message" required></textarea>
    <div class="g-recaptcha" data-sitekey="6LdMM-QaAAAAACxt8fsrNCqF7HFhp-LVFOefKlt5"></div>
    <input type="submit" name="submit" value="Send Messgae" class="submit-btn">          
</form>
    
<div class="status">
<?php

if(isset($_POST['submit'])){
 $full_name = $_POST['name'];
    $phone = $_POST['phone'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];


    $email_from = '[email protected]';
    $email_subject = "New Form Submission";
    $email_body = "Name: $full_name.\n".
                "User phone: $phone.\n".
                "User Email: $visitor_email.\n".
                "User Message: $message.\n";

    $to = "[email protected]";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    
    $secretKey = "6LdMM-QaAAAAABsKlfpA2f4niUVM-_8DpTwYZWVe";
    $responseKey = $_POST['g-recaptcha-response'];
    $userIP = $_SERVER['REMOTE_ADDR'];
    $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
    $response = file_get_contents($url);
    $response = json_decode($response);
    
    if ($response->success)
    {
    mail($to,$email_subject,$email_body,$headers);                       
    echo "Message Sent Successfully";
    }   
    else
    {
    echo "<span>Invalid Captcha, Please Try Again</span>";
    }
}
    
?>
</div>     
</div>
   
</body>
</html>
php recaptcha
2个回答
0
投票

allow_url_fopen 设置在您的服务器上被禁用,这可能是由于托管提供商为增强服务器安全性而采取的安全措施和服务器配置而发生的。这就是 file_get_contents 函数不起作用的原因。你尝试用curl代替file_get_contents。

 $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['secret' => $secretKey, 'response' => $response, 'remoteip' => $userIP]));
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    $response = curl_exec($ch);
                    curl_close($ch);

                    $responseData = json_decode($response);

-1
投票

根据文档中的验证用户的响应章节,您需要提交POST请求

有了

file_get_contents()
,它会是这样的:

$url = 'https://www.google.com/recaptcha/api/siteverify';
$post = [
    'secret' => $secretKey,
    'response' => $responseKey,
    'remoteip' => $_SERVER['REMOTE_ADDR'],
];
$options = [
    'http' => [
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query($post, null, '&', PHP_QUERY_RFC3986),
    ]
];
$response = file_get_contents($url, false, stream_context_create($options));
if ($response===false) {
    // Handle error here
}else{
    // Handle response here
}
© www.soinside.com 2019 - 2024. All rights reserved.