Answers for "verify recaptcha 3 php"

PHP
0

verify recaptcha 3 php

private function verify_reCaptcha(){
		$success = false;
		try {
			$url = 'https://www.google.com/recaptcha/api/siteverify';
			$data = [
				'secret'   => "secret key",
				'response' => "token_reCaptcha_from_client",
				'remoteip' => $_SERVER['REMOTE_ADDR']
			];
			
			$options = [
				'http' => [
					'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
					'method'  => 'POST',
					'content' => http_build_query($data)
				]
			];
			
			$context  = stream_context_create($options);
			$result = file_get_contents($url, false, $context);
			
			log_message("error", "reCaptcha ".print_r([$url, $data, $options, $result],true));
			if($res = json_decode($result)) {
				if ($res->success){
					$success = true;
					$message = "success";
				}else{
					$message = "reCaptcha validation failed";
				}
			} else {
				$message = "failed to decode json";
			}
		} catch (Exception $e) {
			$message = $e->getMessage();
			send_report("reCaptcha Failed : auth.php\n{$message}");
		}
		return ["success" => $success, "message" => $message];
	}
Posted by: Guest on August-26-2021

Browse Popular Code Answers by Language