43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
require_once APPPATH . 'controllers/api/BaseController.php';
|
|
|
|
/**
|
|
* Notes:生成验证码
|
|
* Created on: 2022/8/29 17:15
|
|
* Created by: dengbw
|
|
*/
|
|
class Captcha extends BaseController
|
|
{
|
|
private $redis;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->redis = &load_cache('redis');
|
|
}
|
|
|
|
public function index_get()
|
|
{
|
|
$this->load->library('MyCaptcha');
|
|
$myCaptcha = new MyCaptcha();
|
|
$re = $myCaptcha->create(5, ['width' => 16, 'height' => 20, 'pnum' => 50, 'base64' => 1]);
|
|
$code_key = $this->generateSign();
|
|
$this->redis->save($code_key, $re['text'], 5 * 60);
|
|
$data = ['base64' => $re['base64'], 'code_key' => $code_key];
|
|
$this->return_response($data);
|
|
}
|
|
|
|
function generateSign()
|
|
{
|
|
$sign = md5(mt_rand() . 'mycaptchamarket');
|
|
// 拼接上签名作为 Redis 的 key
|
|
$key = 'code_' . $sign;
|
|
if ($this->redis->exists($key)) {
|
|
// 如果生成的 Sign 已存在,就进行递归,直到生成出一个不存在的。
|
|
return $this->generateSign();
|
|
}
|
|
return $key;
|
|
}
|
|
|
|
} |