Home  >  Article  >  Backend Development  >  Share a PHP free verification code (with code)

Share a PHP free verification code (with code)

藏色散人
藏色散人forward
2023-04-23 17:33:211790browse

Preface

CAPTCHA is the abbreviation of "Completely Automated Public Turing test to tell Computers and Humans Apart", It is a public, fully automatic program that distinguishes whether the user is a computer or a human.

Share a PHP free verification code (with code)

Front-end code

<script src="captcha.js?appid=xxx"></script>

<script>
kg.captcha({
    // 绑定元素,验证框显示区域
    bind: "#captchaBox",

    // 验证成功事务处理
    success: function(e) {
        console.log(e);
    },

    // 验证失败事务处理
    failure: function(e) {
        console.log(e);
    },

    // 点击刷新按钮时触发
    refresh: function(e) {
        console.log(e);
    }
});
</script>

<div id="captchaBox">载入中 ...</div>

PHP code

<?php
include "public/KgCaptchaSDK.php";

// 填写你的 AppId,在应用管理中获取
$appId = "appId";

// 填写你的 AppSecret,在应用管理中获取
$appSecret = "appSecret";

$request = new kgCaptcha($appId, $appSecret);
// 填写应用服务域名,在应用管理中获取
$request->appCdn = "https://cdn.kgcaptcha.com";

// 前端验证成功后颁发的 token,有效期为两分钟
$request->token = $_POST["kgCaptchaToken"];

// 当安全策略中的防控等级为3时必须填写
$request->userId = "kgCaptchaDemo";

// 请求超时时间,秒
$request->connectTimeout = 10;

$requestResult = $request->sendRequest();
if ($requestResult->code === 0) {
    // 验签成功逻辑处理
    echo "验证通过";
} else {
    // 验签失败逻辑处理
    echo "验证失败,错误代码:{$requestResult->code}, 错误信息:{$requestResult->msg}";
}

Effect display

Related links

SDK open source address: github.com/KgCaptcha, by the way A demonstration: www.kgcaptcha.com/demo/

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Share a PHP free verification code (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete