一个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 如何获取文件的后缀名

比如图片文件的后缀,jpg或gif等 有两个方法 一,假如$img为图片文件名 $img=12345.gif; $img_ext = substr($img, strrpos($im...

Php output buffering缓存及程序缓存深入解析

Php output buffering缓存及程序缓存深入解析

下面测试ob缓存和程序缓存:在测试前为了测试效果更明显,我们在php.ini里先关闭ob缓存并设置明显的错误级别。Output_buffering=offDisplay_errors=o...

PHP基于单例模式编写PDO类的方法

一、单例模式简介 简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务; 二、为什么要使用PHP单例模式?    &n...

PHP中使用mktime获取时间戳的一个黑色幽默分析

下面这段代码是网上大多数人给出的时间戳现实,这个一看便知只能说是取得当前日期,而不能算是时间戳,不用多解释了吧! 复制代码 代码如下: $now = mktime(0,0,0,date(...

php中使用getimagesize获取图片、flash等文件的尺寸信息实例

如果你还想着通过解析swf文件头信息来获取flash文件的尺寸信息,那真的有点走远了。因为从PHP 4开始已经内置getimagesize函数来做这个事。其功能测定任何 GIF,JPG,...