Add a beautiful PHP Verification Code class to favorites

Source: Internet
Author: User

The purpose of the verification code is needless to say. I also wrote an article about the PHP verification code, but it is not encapsulated into a class. The following describes a PHP Verification Code class.

<?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;?>

After being encapsulated into a class, constructor is added for ease of use. You can also continue to improve the verification code class, such as adding destructor and saving memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.