首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ PHP / 文章详情

一个PHP实现的验证码库并生成文件记录

Sonder
2019-10-14
1171字
3分钟
浏览 (2.7k)

Build Status

PHP生成验证码图片

PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:

1、产生一张png的图片;
2、为图片设置背景色;
3、设置字体颜色和样式;
4、产生指定位数的随机的验证码;
5、把产生的每个字符调整旋转角度和位置画到png图片上;
6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;
7、输出图片;
8、释放图片所占内存。

安装完成之后

index.php

复制代码
<?php
require 'vendor/autoload.php';
defined('DIR_FILE') or define('DIR_FILE', dirname(__FILE__));
use Minho\Captcha\CaptchaBuilder;

//session_start();
$captch = new CaptchaBuilder();

$captch->initialize([
    'width' => 150,     // 宽度
    'height' => 50,     // 高度
    'line' => false,    // 直线
    'curve' => true,    // 曲线
    'noise' => 1,       // 噪点背景
    'fonts' => []       // 字体
]);

$captch->create();
$captch->output(1);
$content = [
    'overtime' => time() + 180,
    'captcha' => $captch->getText()
];
file_put_contents(DIR_FILE.'/file/1.captcha', json_encode($content));
//$_SESSION['captch'] = $captch->getText();//存入session

getContent.php

复制代码
<?php
defined('DIR_FILE') or define('DIR_FILE', dirname(__FILE__));
//session_start();
//echo $_SESSION['captch'];

$c = file_get_contents(DIR_FILE.'/file/1.captcha');
$a = json_decode($c,true);
print_r($a);
下一篇 / JS笔记

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)