[Function] Recaptcha Google

admin

Administrator
Staff member
verify_recaptcha(responseKey, IP)
The verify_recaptcha(responseKey, IP) function is used to validate Google reCAPTCHA on the server-side. It sends a request to Google's API to verify the validity of the response key (responseKey) from the client and the user's IP address.

Parameters:
  • responseKey: The response key from the client (sent from the form when the user completes reCAPTCHA).
  • IP: The user's IP address.
How it works:
  1. Add the secret key to the site's settings.photo_2025-04-30_17-33-26.jpg
  2. The request includes:
    • secret (the site's secret key, provided by Google).
    • response (the responseKey value).
    • remoteip (the IP value, optional).
  3. The API returns a JSON response with a success field:
    • true: Verification successful.
    • false: Verification failed, with error codes (if any).
Example:
{% if request_method()|lower == 'post' %}
{% set recaptcha = get_post('g-recaptcha-response') %}

{% if recaptcha is null %}
<p class="alert alert-danger">Please complete the robot verification.</p>
{% elseif verify_recaptcha(recaptcha, ip()) %}
{% set user = get_data_by_id('users', 1).data %}
{% set input_nick = get_post('nick')|lower %}
{% set input_pass = get_post('pass')|lower %}

{% if user['nick'] == input_nick and user['pass'] == md5(input_pass) %}
{% do set_cookie('token', user['pass']) %}
{{ redirect('/manager') }}
{% else %}
<p class="alert alert-warning">
Invalid login credentials. Please try again.
</p>
{% endif %}
{% else %}
<p class="alert alert-danger">
Verification failed. Please complete the robot verification.
</p>
{% endif %}
{% endif %}
 
Hàm verify_recaptcha(responseKey, IP) được sử dụng để xác thực Google reCAPTCHA trên phía máy chủ. Hàm này gửi yêu cầu đến API của Google để xác minh tính hợp lệ của khóa phản hồi (responseKey) từ máy khách và địa chỉ IP của người dùng.

Các thông số:
  • responseKey : Khóa phản hồi từ máy khách (được gửi từ biểu mẫu khi người dùng hoàn tất reCAPTCHA).
  • IP : Địa chỉ IP của người dùng.
Cách thức hoạt động:
  1. Thêm khóa bí mật vào phần cài đặt của trang web.View attachment 22
  2. Yêu cầu bao gồm:
    • bí mật (khóa bí mật của trang web do Google cung cấp).
    • phản hồi (giá trị responseKey).
    • remoteip (giá trị IP, tùy chọn).
  3. API trả về phản hồi JSON với trường thành công:
    • đúng: Xác minh thành công.
    • sai: Xác minh không thành công, kèm theo mã lỗi (nếu có).
Ví dụ:
I tested but it didn't work
 
Back
Top