小程序不支持session,php使用apcu扩展实现验证码功能
2024-10-15 Umming 极致cms 评论(0) 浏览(854)
function apcucode(){
header('Content-Type: image/png');
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < 4; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
$randomString = strtoupper($randomString);
apcu_store('apcucode',md5(md5($randomString)), 60);
$image = imagecreatetruecolor(120, 40);
$white = imagecolorallocate($image, rand(210,255),rand(210,255),rand(210,255));
imagefill($image, 0, 0, $white);
$font = APP_PATH."frphp/extend/AdobeGothicStd-Bold.ttf";
$x = 10;
$y = 30;
for ($i = 0; $i < 4; $i++) {
$textColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
imagettftext($image, 20, 0, $x, $y, $textColor, $font, $randomString[$i]);
$x += 27;
}
for ($i = 0; $i < 5; $i++) {
$lineColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
imageline($image, rand(0, 120), rand(0, 40), rand(0, 120), rand(0, 40), $lineColor);
}
imagepng($image);
imagedestroy($image);
}
function message(){
if($_POST){
$userProvidedCode = $this->frparam('vercode',1,'','POST');
$userProvidedCode =md5(md5(strtoupper($userProvidedCode)));
$storedCode = apcu_fetch('apcucode');
if (strcasecmp($userProvidedCode ,$storedCode) == 0) {
// 码正确,执行请求
JsonReturn(['code'=>0,'msg'=>'验证码正确!']);
} else {
// 码错误,返回错误提示
JsonReturn(['code'=>1,'msg'=>'验证码不正确']);
}
}else{
JsonReturn(['code'=>1,'msg'=>'请求方法错误!']);
}
} 标签: php代码
本文链接:https://www.umming.com/jizhicms/333.html
声明:本站信息由网友自行发布或来源于网络,真实性、合法性由发布人负责,请仔细甄别!本站只为传递信息,我们不做任何双方证明,也不承担任何法律责任。文章内容若侵犯你的权益,请联系本站删除!
也许你还会对下面的内容感兴趣:
发表评论: