Share a PHP verification code class (encapsulated as a class) _ PHP Tutorial

Source: Internet
Author: User
Share a PHP verification code class (encapsulated as a class ). Copy the code as follows :? Phpsession_start (); Header (Content-type: imagegif); classSecurityCode {private $ codes; function _ construct () {$ code0-1-2-3-4-5-6-7-8-9- The code is as follows:


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-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, 255 );
$ Testcolor4 = imagecolorallocate ($ img, 255, 0, 255 );
For ($ I = 0; $ I <4; $ I ++)
{
Imagestring ($ img, rand (), 8 + $ I * 15, rand (), $ this-> codes [$ I], rand ));
}
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.

The http://www.bkjia.com/PHPjc/323972.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323972.htmlTechArticle code is as follows :? 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 -...

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.