PHP verification code performance implementation

Source: Internet
Author: User
Tags random seed
PHP verification code function implementation/*** generate verification code image */publicfunctionactionVerfiycode () {Header ("Content-type: image/gif"); $ border = 0; PHP verification code
/*** Generate verification code image */public function actionVerfiycode () {Header ("Content-type: image/gif"); $ border = 0; // Border 1 required: 0 not $ how = 4; // Number of digits of the verification code $ w = $ how * 15; // image width $ h = 20; // Image Height $ fontsize = 5; // font size $ alpha = "abcdefghijkmnopqrstuvwxyz"; // verification code content 1: letter $ number = "0123456789"; // verification code content 2: number $ randcode = ""; // verification code string initialization srand (double) microtime () * 1000000); // initialize random seed $ im = ImageCreate ($ w, $ h); // Create a verification image $ bgcolo R = ImageColorAllocate ($ im, 255,255,255); // set the background color ImageFill ($ im, 0, 0, $ bgcolor); // fill the background color if ($ border) {$ black = ImageColorAllocate ($ im, 0, 0, 0); // set the border color ImageRectangle ($ im, 0, 0, $ w-1, $ h-1, $ black); // draw a border} for ($ I = 0; $ I <$ how; $ I ++) {$ alpha_or_number = mt_rand (0, 1 ); // letter or number $ str = $ alpha_or_number? $ Alpha: $ number; $ which = mt_rand (0, strlen ($ str)-1); // specify the character $ code = substr ($ str, $ which, 1); // get the character $ j =! $ I? 4: $ j + 15; // The position of the painted character $ color3 = ImageColorAllocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100 )); // the color of the character ImageChar ($ im, $ fontsize, $ j, 3, $ code, $ color3); // draw the character $ randcode. = $ code; // add the verification code string to the string by bit} for ($ I = 0; $ I <5; $ I ++) // paint the background interference line {$ color1 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 )); // interference line color ImageArc ($ im, mt_rand (-5, $ w), mt_rand (-5, $ h), mt_rand (20,300), mt_rand (20,200), 55, 44, $ color1); // interference line} for ($ I = 0; $ I <$ how * 40; $ I ++) // paint background interference points {$ color2 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 )); // disturbance point color ImageSetPixel ($ im, mt_rand (0, $ w), mt_rand (0, $ h), $ color2 ); // interference point} $ session = new CHttpSession; $ session-> open (); Yii: app ()-> session-> add ('randcode', $ randcode ); /* drawing end */Imagegif ($ im); ImageDestroy ($ im);/* drawing end */}


From: http://www.admin5.com/article/20080314/75984.shtml

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.