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-sitekeyin your HTML (6LdMM-QaAAAAACxt8fsrNCqF7HFhp-LVFOefKlt5) and the$secretKeyin 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_contentswith cURL: Thefile_get_contents()function often fails ifallow_url_fopenis 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:
If// Check if the reCAPTCHA response is being submitted var_dump($_POST['g-recaptcha-response']); // View Google's full validation response var_dump($response);$_POST['g-recaptcha-response']is empty, the user didn’t complete the challenge or the widget failed to load. If Google’s response includeserror-codes, refer to reCAPTCHA’s error code docs for specific fixes (e.g.,invalid-input-secretmeans your secret key is incorrect). - Check Form Submission: Ensure no JavaScript on your page is blocking the
g-recaptcha-responsefield 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 withfile_get_contents): Check ifallow_url_fopen = Onin yourphp.inifile (you can confirm this with aphpinfo()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:
If this returns an error, your server’s firewall or hosting provider may be blocking access—contact them to resolve this.curl https://www.google.com/recaptcha/api/siteverify - PHP Version Compatibility: Ensure you’re running PHP 5.4 or newer (required for proper
json_decodebehavior). Most modern servers meet this requirement, but it’s worth confirming.
Next Steps
- First, double-check your API keys and authorized domains in the Google Admin Console.
- Replace
file_get_contentswith the cURL code above. - Add debug outputs to see the exact response from Google.
- 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




