You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

集成reCaptcha v2后PHP+AJAX表单无法运行PHP脚本求助

Hey there, let's break down this reCaptcha v2 + PHP 5.6 issue step by step—since your HTML and AJAX are working fine but the PHP validation/email flow is failing, we can narrow the problem down to server-side details that are easy to miss. Here are the most likely culprits and how to test them:

1. ReCaptcha API Request Quirks in PHP 5.6

PHP 5.6 has some SSL and cURL behavior that differs from newer versions, which often trips up reCaptcha verification:

  • Double-check your keys: Make sure you're using the server-side secret key (not the public site key) in your PHP script. Mixing these up is the #1 mistake here.
  • Fix cURL SSL verification: PHP 5.6 might not trust Google's SSL certificates if your server has an outdated CA bundle. Add explicit SSL settings to your cURL call:
    $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
        'secret' => 'YOUR_SERVER_SECRET_KEY',
        'response' => $_POST['g-recaptcha-response'],
        'remoteip' => $_SERVER['REMOTE_ADDR']
    ]));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Critical for PHP 5.6: Ensure SSL verification works
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cacert.pem'); // Use your server's CA bundle path
    
  • Add error logging for cURL: If the API request fails silently, log the error to debug:
    if(curl_errno($ch)) {
        error_log('ReCaptcha cURL error: ' . curl_error($ch));
        // Return failure to your AJAX handler here
    }
    $apiResponse = curl_exec($ch);
    curl_close($ch);
    
  • Validate the JSON response: PHP 5.6 supports json_decode, but if the API response is malformed, $result will be null. Check this:
    $result = json_decode($apiResponse);
    if(is_null($result)) {
        error_log('Invalid ReCaptcha API response: ' . $apiResponse);
    }
    
2. Confirm the ReCaptcha Response Reaches PHP

Even if your AJAX looks correct, the g-recaptcha-response parameter might not be making it to your script. Add a log at the start of your PHP file to verify:

error_log('Received POST data: ' . print_r($_POST, true));

If g-recaptcha-response is missing, double-check your AJAX code to ensure it's calling grecaptcha.getResponse() correctly and including the value in the form data.

3. Email Sending Failures (Post-ReCaptcha)

Sometimes reCaptcha passes, but the email logic breaks. Add strict error checking here:

  • For the native mail() function:
    $mailSent = mail($to, $subject, $message, $headers);
    if(!$mailSent) {
        error_log('Email failed: ' . error_get_last()['message']);
    }
    
  • PHP 5.6 has stricter header validation—make sure your From address is allowed by your server (no fake domains) and headers don't have extra newlines.
4. Server Configuration Checks
  • Ensure allow_url_fopen is enabled if you're using file_get_contents instead of cURL (cURL is more reliable for reCaptcha, though).
  • Check your Apache/PHP error logs (usually /var/log/apache2/error.log or similar) for hidden warnings/fatal errors that aren't showing up on the frontend.
  • Verify PHP 5.6 has required extensions: json, curl, and openssl (all should be enabled by default, but it's worth confirming with phpinfo()).
5. Test ReCaptcha in Isolation

Create a minimal PHP script to test only reCaptcha verification (separate from your email code) to rule out where the failure happens:

<?php
$secret = 'YOUR_SERVER_SECRET_KEY';
$response = $_POST['g-recaptcha-response'];

$ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'secret' => $secret,
    'response' => $response,
    'remoteip' => $_SERVER['REMOTE_ADDR']
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

$apiResponse = curl_exec($ch);
curl_close($ch);

$result = json_decode($apiResponse);
var_dump($result);
?>

Submit a test request with a valid reCaptcha response—if $result->success is true, the issue is in your email logic, not reCaptcha.

If you can share snippets of your actual PHP/HTML code, we can pinpoint the exact issue even faster!

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:05:37