一个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快速排序算法实现的原理及代码详解

PHP快速排序算法实现的原理及代码详解

算法原理 下列动图来自五分钟学算法,演示了快速排序算法的原理和步骤。 步骤: 从数组中选个基准值 将数组中大于基准值的放同一边、小于基准值的放另一边,基准值位于中间位置 递归的对分...

php数组函数序列之next() - 移动数组内部指针到下一个元素的位置,并返回该元素值

next() 定义和用法 next() 函数把指向当前元素的指针移动到下一个元素的位置,并返回该元素的值。 如果内部指针已经超过数组的最后一个元素,函数返回 false。 语法 next...

PHP实现微信JS-SDK接口选择相册及拍照并上传的方法

PHP实现微信JS-SDK接口选择相册及拍照并上传的方法

本文实例讲述了PHP实现微信JS-SDK接口选择相册及拍照并上传的方法。分享给大家供大家参考,具体如下: 理解:微信上传接口是拍照,或者选择本地照片,上传到微信的服务器,获取到一个id,...

discuz authcode 经典php加密解密函数解析

原理如下,假如: 加密 明文:1010 1001 密匙:1110 0011 密文:0100 1010 得出密文0100 1010,解密之需和密匙异或下就可以了 解密 密文:0100 10...

php实现对文件压缩简单的方法

压缩一个文件 我们将一个文件生成一个压缩包。 <?php $path = "c:/wamp/www/log.txt"; $filename = "test.zip"...