Home >Backend Development >PHP Tutorial >一个已封装成类的PHP验证码类分享

一个已封装成类的PHP验证码类分享

PHP中文网
PHP中文网Original
2017-07-15 17:41:33975browse

一个已封装成类的PHP验证码类分享

<?php 
session_start(); 
Header("Content-type: image/gif"); 
class SecurityCode 
{ 
private $codes = &#39;&#39;; 
function __construct() 
{ 
$code = &#39;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&#39;; 
$codeArray = explode(&#39;-&#39;,$code); 
shuffle($codeArray); 
$this->codes = implode(&#39;&#39;,array_slice($codeArray,0,4)); 
} 
public function CreateImg() 
{ 
$_SESSION[&#39;check_pic&#39;] = $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; 
?>


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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn