You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

PHP集成Google Recaptcha V2始终提示‘Invalid Captcha, Please Try Again’问题排查求助

Troubleshooting "Invalid Captcha, Please Try Again" in Google reCAPTCHA V2 Integration

Let’s walk through the possible causes of your reCAPTCHA validation failure, covering API configuration, code logic, and server environment issues:

1. API Configuration Checks

First, rule out the most common misconfiguration mistakes:

  • Verify Site Key & Secret Key Match: Ensure the data-sitekey in your HTML (6LdMM-QaAAAAACxt8fsrNCqF7HFhp-LVFOefKlt5) and the $secretKey in your PHP (6LdMM-QaAAAAABsKlfpA2f4niUVM-_8DpTwYZWVe) belong to the same reCAPTCHA V2 instance in the Google reCAPTCHA Admin Console. Double-check you didn’t mix up V2 and V3 keys—they aren’t interchangeable.
  • Validate Authorized Domains: In the Admin Console, confirm that nexteknologi.com (and any subdomains if needed) is listed under "Authorized domains" for your reCAPTCHA project. Even a small typo here will block validation.

2. Code Logic Issues

Your validation flow has a few potential weak points to address:

  • Replace file_get_contents with cURL: The file_get_contents() function often fails if allow_url_fopen is disabled (a common security setting on many servers). cURL is more reliable for external API calls—use this updated code:
    $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";
    
    // Use cURL instead of file_get_contents
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Keep SSL verification enabled for security
    $response = curl_exec($ch);
    curl_close($ch);
    
    $response = json_decode($response);
    
  • Add Debug Outputs: Insert these lines before the if ($response->success) check to see exactly what’s happening:
    // Check if the reCAPTCHA response is being submitted
    var_dump($_POST['g-recaptcha-response']);
    // View Google's full validation response
    var_dump($response);
    
    If $_POST['g-recaptcha-response'] is empty, the user didn’t complete the challenge or the widget failed to load. If Google’s response includes error-codes, refer to reCAPTCHA’s error code docs for specific fixes (e.g., invalid-input-secret means your secret key is incorrect).
  • Check Form Submission: Ensure no JavaScript on your page is blocking the g-recaptcha-response field from being sent. Use your browser’s dev tools (Network tab) to confirm the field is included in the POST data when submitting the form.

3. Server Environment Checks

  • Verify allow_url_fopen (if sticking with file_get_contents): Check if allow_url_fopen = On in your php.ini file (you can confirm this with a phpinfo() page).
  • Test Network Access to Google’s API: Some servers block outgoing requests to external services. Run this command in your server’s terminal to check connectivity:
    curl https://www.google.com/recaptcha/api/siteverify
    
    If this returns an error, your server’s firewall or hosting provider may be blocking access—contact them to resolve this.
  • PHP Version Compatibility: Ensure you’re running PHP 5.4 or newer (required for proper json_decode behavior). Most modern servers meet this requirement, but it’s worth confirming.

Next Steps

  1. First, double-check your API keys and authorized domains in the Google Admin Console.
  2. Replace file_get_contents with the cURL code above.
  3. Add debug outputs to see the exact response from Google.
  4. Test if your server can reach Google’s API endpoint.

These steps should help you pinpoint whether the issue is with configuration, code, or environment.

内容的提问来源于stack exchange,提问作者Angga

火山引擎 最新活动