一个PHP验证码类代码分享(已封装成类)

yipeiwu_com6年前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,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;
?>

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

相关文章

用php解析html的实现代码

最近想用php写一个爬虫,就需要解析html,在sourceforge上找到一个项目叫做PHP Simple HTML DOM Parser,它可以以类似jQuery的方式通过css选择...

用PHP实现递归循环每一个目录

函数的原理很简单,主要就是用了一下递归调用。 复制代码 代码如下: function file_list($path){ if ($handle = opendir($path)) {...

php遍历所有文件及文件夹的方法深入解析

 1.方法一:复制代码 代码如下:<? $dir="D:"; static $dir_list =0; static $file_list =0; function li...

[企业公众号]升级到[企业微信]之后发送消息失败的解决方法

本文实例讲述了[企业公众号]升级到[企业微信]之后发送消息失败的解决方法。分享给大家供大家参考,具体如下: 最近,看到消息,说是微信把企业公众号升级到企业微信。当时并没有在意,但是后来发...

php将日期格式转换成xx天前的格式

本文实例讲述了php将日期格式转换成xx天前格式的方法。分享给大家供大家参考。具体如下: 这段代码可以把时间格式化成3天前,5秒前,2年前的形式 // convert a date...