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

yipeiwu_com5年前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下拉列表的方法。分享给大家供大家参考。具体如下: 这段代码可根据定义好的php数组动态生成一个html的下拉列表(select) <?...

Admin generator, filters and I18n

Three easy steps 1) configure function Add an input for each field you want to include in you...

PHP无刷新上传文件实现代码

index.html 复制代码 代码如下: <html> <head> <title>无刷新上传文件</title> <meta C...

一个显示某段时间内每个月的方法 返回由这些月份组成的数组

复制代码 代码如下: /** * 生成从开始月份到结束月份的月份数组 * 该方法仿照党子皓getDateArr()方法 * @param unknown_type $start * @p...

spl_autoload_register与autoload的区别详解

spl_autoload_register(PHP 5 >= 5.1.2)spl_autoload_register — 注册__autoload()函数说明bool spl_au...