· 5 years ago · May 23, 2020, 01:40 PM
1//фрагмент класса каптчи
2<?php
3/**
4 * @file recaptcha.class.php
5 * Created by PhpStorm.
6 * @author RFO-DEV on май, 2020
7 *
8 **/
9
10class Recaptcha
11{
12 /**
13 * @var string
14 */
15 private $site_key = '6LcXUPsUAAAAAPKOpmnWQTAq6sWSOz7uIPUpiuy8';//v2
16 /**
17 * @var string
18 */
19 private $secret_key = '6LcXUPsUAAAAAEidhcqV9qBTI-l9VH3QuvJSvb6I'; //v2
20
21 /**
22 * @var
23 */
24 private $Return;
25
26 /**
27 * Recaptcha constructor.
28 */
29 public function __construct(){}
30
31 /**
32 * @param $SecretKey
33 * @return mixed
34 */
35 public function getCaptcha($SecretKey)
36 {
37 $url = 'https://www.google.com/recaptcha/api/siteverify';
38 $key = $this->secret_key;
39 $query = $url.'?secret='.$key.'&response='.$_POST['g-recaptcha-response'].'&remoteip='.$_SERVER['REMOTE_ADDR'];
40 $Response = file_get_contents($query);
41 $this->Return = json_decode($Response);
42 return $this->Return;
43 }
44
45
46 /**
47 * @return string
48 */
49 public function getSiteKey()
50 {
51 return $this->site_key;
52 }
53
54 /**
55 * @return string
56 */
57 public function getSecretKey()
58 {
59 return $this->secret_key;
60 }
61}
62
63//класс USerController
64<?php
65
66/*
67 * To change this license header, choose License Headers in Project Properties.
68 * To change this template file, choose Tools | Templates
69 * and open the template in the editor.
70 */
71
72/**
73 * Description of users
74 *
75 * @author RFDEVELOP
76 */
77class UsersController extends Controller
78{
79
80 /**
81 * @var bool
82 */
83 private $result = false;
84
85 /**
86 * UsersController constructor.
87 * @param array $data
88 */
89 public function __construct($data = array())
90 {
91 parent::__construct($data);
92 $this->model = new User();
93 }
94
95 public function register()
96 {
97 if (isset($_POST['g-recaptcha-response'])) {
98
99 $res = App::$recaptcha->getCaptcha($_POST['g-recaptcha-response']);
100
101 if ($res->success == true && $res->score > 0.5) {
102
103 if ($_POST && isset($_POST ['login']) && isset($_POST ['email']) && isset($_POST ['password'])) {
104 $this->result = $this->model->register($_POST);
105 }
106 if ($this->result) {
107 Flash::setFlash('Ваш аккаунт успешно создан!');
108 Router::redirect('/');
109 } else {
110 Flash::setFlash('Ошибка: Не удалось создать аккаунт!');
111 // Router::redirect('/');
112 }
113 }
114 }
115 }
116...
117}
118
119//код страницы реги с капчей
120<script src='https://www.google.com/recaptcha/api.js'></script>
121<div class="right_box_container">
122 <div class="forms_container">
123 <form method="POST" action="">
124 <div class="form-group">
125 <div class="form-group">
126 <h3>User register page</h3>
127 <label>Login</label>
128 <input type="text" name="login" placeholder="login" required/>
129 <label>Email</label>
130 <input type="email" name="login" placeholder="email" required/>
131 <label>Password</label>
132 <input type="password" name="password" placeholder="password" required/>
133<!-- <input type="text" id="g-recaptcha-response" name="g-recaptcha-response"/>-->
134 <div class="g-recaptcha" data-sitekey="6LcXUPsUAAAAAPKOpmnWQTAq6sWSOz7uIPUpiuy8"></div>
135 <div class="btn">
136 <!-- <input type="reset" class="btn-login" value="clear"/>-->
137 <input type="submit" name="submit" class="btn-login" value="register"/>
138
139 </div>
140
141 </div>
142 </div>
143 </form>
144 </div>
145 <!-- /.forms_container -->
146</div>