一个PHP验证码类代码分享(已封装成类)
更新时间:2011年07月17日 13:05:02 作者:
验证码的用途就不用多说了,之前也写了一篇关于PHP验证码的文章,PHP 验证码的实现。但是没有封装成类。下面就介绍一个PHP的一个验证码类。
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
复制代码 代码如下:
<?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,0,4));
}
public function CreateImg()
{
$_SESSION['check_pic'] = $this->codes;
$img = imagecreate(70,25);
imagecolorallocate($img,222,222,222);
$testcolor1 = imagecolorallocate($img,255,0,0);
$testcolor2 = imagecolorallocate($img,51,51,51);
$testcolor3 = imagecolorallocate($img,0,0,255);
$testcolor4 = imagecolorallocate($img,255,0,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;
?>
封装成类之后,加入了构造函数,使用起来也方便些。你也可以继续完善下这个验证码类,比如加入析构函数,如何更节省内存等等。
相关文章
Admin generator, filters and I18n
You need to modify your EntityFormFilter (where Entity is your object class - Article, Book, etc.).2011-10-10php读取csv文件后,uft8 bom导致在页面上显示出现问题的解决方法
以下是对php读取csv文件后,uft8 bom导致在页面上显示出现问题的解决方法进行了详细的分析介绍,需要的朋友可以过来参考下2013-08-08对squid中refresh_pattern的一些理解和建议
用于确定一个页面进入cache后,它在cache中停留的时间。refresh_pattern规则仅仅应用到没有明确过时期限的响应。原始服务器能使用Expires头部,或者Cache-Control:max-age指令来指定过时期限。2009-04-04
最新评论