muhammetsafak/captcha

PHP 基础验证码

dev-main 2022-04-23 03:41 UTC

This package is auto-updated.

Last update: 2024-09-24 11:45:58 UTC


README

这是一个传统的验证码应用。

php-basic-captcha

要求

  • PHP 5.6 或更高版本
  • PHP GD 扩展

安装

composer require muhammetsafak/captcha:dev-main

或下载并包含文件 src/Captcha.php

使用方法

文件 src/image-captcha.php 简单地生成并展示验证码。您可以编辑此文件进行自定义配置或创建自己的文件。

配置

$configs = array(
    'width'         => 200, // Width of captcha image to be created
    'height'        => 70, // Height of captcha image to be created
    'length'        => 7, // The number of characters to be found in the captcha image.
    'font'          => __DIR__ . '/src/AHGBold.ttf', // Defines the font of the captcha characters.
    'size'          => 18 // Defines the size of captcha characters.
);

要创建验证码图片;image-captcha.php

require_once __DIR__ . '/src/Captcha.php';
$config = array(
    'width'         => 180,
    'height'        => 50,
    'length'        => 6
);
$captcha = new \MuhammetSafak\Captcha\Captcha($config);
$captcha->create();
<form method="POST">
    <input type="text" name="your_name" placeholder="Your Name"/><br />
    
    <img src="src/image-captcha.php" id="captcha" /> <br />
    <button onclick="captcha_refresh()">Refresh</button>
    <input type="text" name="areYouBot" placeholder="Captcha" /><br />
    
    <input type="submit" value="Gönder" />
</form>
<script>
    function captcha_refresh()
    {
        img = document.getElementById("captcha");
        img.src = '../src/image-captcha.php';
    }
</script>

一个简单的验证示例如下。

require_once __DIR__ . '/src/Captcha.php';
$captcha = new \MuhammetSafak\Captcha\Captcha();
if(isset($_POST)){
    if($captcha->verify($_POST['areYouBot'])){
        // Success
        // ...process
    }else{
        // Error
        echo 'Enter the characters in the picture correctly.';
    }
}

许可证

版权 © 2022 MIT 许可证