加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

分享一个生成图片验证码的php类

发布时间:2020-05-25 02:29:41 所属栏目:PHP 来源:互联网
导读:验证码这样的功能可以说是无处不在了,本文章向大家分享一个使用PHP来实现验证码功能的类,这里我是将验证码实现抽取到一个类中独立开来,那么后面如果再使用到验证码功能,直接引入该类文件并创建该类的实例,就可以使用验证码了。

验证码的用途就不用多说了,下面就介绍一个PHP的验证码类。

session_start();

Header("Content-type: image/gif");

class SecurityCode

{

private $codes = '';

function __construct()

{

$code = '0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z';

$codeArray = explode('-',$code);

shuffle($codeArray);

$this->codes = implode('',array_slice($codeArray,4));

}

public function CreateImg()

{

$_SESSION['check_pic'] = $this->codes;

$img = imagecreate(70,25);

imagecolorallocate($img,222,222);

$testcolor1 = imagecolorallocate($img,255,0);

$testcolor2 = imagecolorallocate($img,51,51);

$testcolor3 = imagecolorallocate($img,255);

$testcolor4 = imagecolorallocate($img,255);

for ($i = 0; $i < 4; $i++)

{

imagestring($img,rand(5,6),8 + $i * 15,rand(2,8),$this->codes[$i],rand(1,4));

}

imagegif($img);

}

}

$code = new SecurityCode();

$code->CreateImg();

$code = NULL;

?>

封装成类之后,加入了构造函数,使用起来也方便些。你也可以继续完善下这个验证码类,比如加入析构函数,如何更节省内存等等。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读