@vlad1753

Как исправить ошибку google recaptcha?

Использую google recaptcha v3.
Код страницы:

<?php
	if(isset($_POST['send'])){
		$site_key = '***********************************************';
		$secret_key = '********************************************';

		function getCaptcha($s){
			$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret_key."&response={$s}"); 
			return json_decode($response);
		}

		

	}

?>
<!DOCTYPE html>
<html>
<head lang="uk">
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Контакт</title>
	<link rel="stylesheet" type="text/css" href="styles/general.css">
	<link rel="stylesheet" type="text/css" href="styles/contact.css">
</head>
<body>
	<div class="wraper">
		<?php include 'pages/header.php'; ?>
		<main>
			<div class="wraper-boxes">
				<div class="box">
					<form method="post" action="">
						<input type="text" placeholder="Ваше имя" name="name" title="Ваше ім'я" required autocomplete="off">
						<input type="email" placeholder="Ваш email" name="email" title="Ваш email" required>
						<textarea name="message" id="message" cols="30" rows="10" required placeholder="Повідомлення"></textarea>
						<input type="text" id="g-recaptcha-response" name="g-recaptcha-response">
						<input type="submit" name="send" class="send" value="Надіслати">
						
					</form>
				</div>
			</div>
		</main>
		<?php include 'pages/footer.php'; ?>
	</div>
	<script src="https://www.google.com/recaptcha/api.js?render=<?php echo $site_key;?>"></script>
	<script>
		grecaptcha.ready(function() {
          grecaptcha.execute('<?php echo $site_key;?>', {action: 'submit'}).then(function(token) {
              // Add your logic to submit to your backend server here.
              console.log(token);
              document.getElementById('g-recaptcha-response').value = token;
          });
        });
	</script>
</body>
</html>


В консоли ошибка:
Error: Invalid reCAPTCHA client id:

Буду благодарен.
  • Вопрос задан
  • 878 просмотров
Решения вопроса 1
@galaxy
Ваш $site_key определен только после сабмита формы через POST
if(isset($_POST['send'])){
  $site_key = '***********************************************';

...

<script>
    grecaptcha.ready(function() {
          grecaptcha.execute('<?php echo $site_key;?>', {action: 'submit'}).then(function(token) {
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы