Home > Backend Development > PHP Tutorial > Collect a more beautiful PHP verification code class_PHP tutorial

Collect a more beautiful PHP verification code class_PHP tutorial

WBOY
Release: 2016-07-13 10:33:02
Original
1171 people have browsed it

Needless to say the purpose of verification code. I also wrote an article about PHP verification code before, but it was not encapsulated into a class. The following is an introduction to a verification code class in 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;
?>
Copy after login

After encapsulating it into a class, a constructor is added, which makes it easier to use. You can also continue to improve this verification code class, such as adding a destructor, how to save memory, etc.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752568.htmlTechArticleNeedless to say the purpose of verification code. I also wrote an article about PHP verification code before, but Not encapsulated into classes. The following is an introduction to a verification code class in PHP. ?php session_s...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template