<html> <head> <title>reCAPTCHA demo: Simple page</title> <script src="https://www.google.com/recaptcha/api.js" async defer></script> </head> <body> <form action="?" method="POST"> <div class="g-recaptcha" data-sitekey="your_site_key"></div> <input type="submit" value="Submit"> </form> </body> </html>
g-recaptcha-response
fieldfile_get_contents
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=". $yoursecret."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']); $googleobj = json_decode($response); $verified = $googleobj->success; if ($verified === true){ //do stuff }
and<div> <ReCAPTCHA sitekey="6LdTD1cnAAAAAK3Qm2TdrpnkgAXRP4NcA__8LHBr" onChange={ captchaOnChange } /> {ctx.captchaError ? <div className="invalid-feedback">{ctx.captchaError}</div> : null} </div>
andimport ReCAPTCHA from "react-google-recaptcha";
const captchaOnChange = (val: unknown): void => { if (val && val !== null) { ctx.setCaptchaOk(true); } };
ErrorStates
of Context.tsx
:
captchaError: string; setCaptchaError: Dispatch<SetStateAction<string>>;
ContextProps
of Context.tsx
:
captchaOk: boolean; setCaptchaOk: Dispatch<SetStateAction<boolean>>;
Provider
of Context.tsx
:
const [captchaOk, setCaptchaOk] = useState<boolean>(false); //Error const [captchaError, setCaptchaError] = useState<string>('');
Context.tsx
captchaOk, setCaptchaOk, captchaError, setCaptchaError,
if (!ctx.captchaOk) { ctx.setCaptchaError('Invalid captcha. Please try again.'); formValid = false; }
<?php $email = $_POST['email']; $curl = curl_init();curl_setopt_array($curl, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL =>'https://www.google.com/recaptcha/api/siteverify', CURL_POST => 1, CURLOPT_POSTFIELDS => [ 'secret' => 'write_you_secret_key_here', 'response' => $_POST['g_recaptcha-response']; ], ]); $response = json_decode(url_exec($curl)); if(!$response->success) { //Redirect with error; write your code } // Register your user
Labels: reCaptcha, Web development