S
S
supercoder6662019-10-01 19:48:58
PHP
supercoder666, 2019-10-01 19:48:58

How to add ReCaptcha?

I'm using RedBean PHP and doing registration, but I'm having trouble adding ReCaptcha. Please help me add it.

<?php 
require "db.php";

$data = $_POST;
if (isset($data['do_signup']) ) 
{

//регистрация

  $errors = array();
  if( trim($data['login']) == '' ) 
  {
    $errors[] = 'Введите логин';
  }


  if( trim($data['email']) == '') 
  {
    $errors[] = 'Введите email';
  }

  if( $data['password'] == '') 
  {
    $errors[] = 'Введите пароль';
  }

  if( $data['password_2'] != $data['password']) 
  {
    $errors[] = 'Пароли не совпадают';
  }

  if(R::count('users', "login = ?", array($data['login'])) > 0 ) 
  {
    $errors[] = 'Пользователь с таким логином уже существует';
  }

  if(R::count('users', "email = ?", array($data['email'])) > 0 ) 
  {
    $errors[] = 'Пользователь с таким email уже существует';
  }


  if ( empty($errors) ) 
  {
      //успешно
    $user = R::dispense('users');
    $user->login = $data['login'];
    $user->email = $data['email'];
    $user->password = password_hash($data['password'], PASSWORD_DEFAULT);
    R::store($user);
    echo '<div class="alert alert-success" role="alert">Вы успешно зарегистрированы!</div>';

  } 
   else 
  {
    echo '<div class="alert alert-danger" role="alert">'.array_shift($errors).'</div>';
  }
}
?>

<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"></head>
<body>


 <div class="container">
    <h1>Регистрация</h1> 
 <form action="signup.php" class="sign-in" method="POST">
 	<div class="form-group">
    <label for="login">Логин</label>
<input type="username" class="form-control" name="login" value="<?php echo @$data['login']; ?>" placeholder="Username" required></div>
<div class="form-group">
    <label for="email">Почта</label>
<input type="email" class="form-control" name="email" value="<?php echo @$data['email']; ?>"  aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">Мы никогда не передаем ваши данные третьим лицам.</small></div>
<div class="form-group">
    <label for="password">Пароль</label>
<input type="password" class="form-control" name="password" placeholder="Password"></div>
<div class="form-group">
    <label for="password">Пароль еше раз</label>
<input type="password" class="form-control" name="password_2" placeholder="Password again"></div>

<button type="submit" name="do_signup" class="btn btn-primary">Регистрация</button>
</form>
</div>
</body>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex-1917, 2019-10-01
@supercoder666

$response = $_POST["g-recaptcha-response"];
      if (empty($response)) {
        //false;
      }
      $url = 'https://www.google.com/recaptcha/api/siteverify';
      $data = array(
        'secret' => '',
        'response' => $response
      );

      $options = [
        'http' => [
        'method' => 'POST',
        'content' => http_build_query($data)
        ]
      ];

      $context  = stream_context_create($options);
      $verify = file_get_contents($url, false, $context);
      $captcha_success = json_decode($verify, true);
        if (!$captcha_success->success) {
          //false
        }

D
Denis L., 2019-10-11
@lisogorsky

In principle, here is a good article on connecting Google reCAPTCHA v3: https://blog.lisogorsky.ru/google-recaptcha-v3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question