A PHP instance of a class that generates random strings and validation codes

Source: Internet
Author: User
Tags lowercase php class php tutorial rand

  This article mainly describes the generation of random strings and Verification code class PHP instance, the need for friends can refer to the

There are a lot of PHP random numbers and code codes with the article, the real application of the few.   Just do it yourself.   Start this section of the PHP tutorial bar, the following implementation of the code, mainly to make a good distinction between a get_code (), another create_check_image (), output image directly after the call, session () to take the verification code directly Get_code ( OK, incidentally, you must put the Session_star () in front of you when using session.   Code as follows:   code as follows: <?php class Randcheckcode {       /* Function name: Get_code ()     &NBSP ;   * Function: Get random string         * Parameters:         1, (int) $length = #随机字符长度         2, (int) $mode = 0     #随机字符类型,         0 for uppercase and digital, 1 for digits, 2 for lowercase, 3 for uppercase letters,   &N Bsp     4 for uppercase and lowercase letters, 5 for capitals and numbers, 6 for lowercase and digital         * return: Obtained string        /  &NB Sp     function Get_code ($length =32, $mode =0)//Get random captcha function         {        & nbsp       switch ($mode)                 {                    &NBsp   Case ' 1 ':                                 $STR = ' 123456789 ';                                 break;                         case ' 2 ':                                 $STR = ' abcdefghijklmnopqrstuvwxyz ';                                 break;                         case ' 3 ':                                 $STR = ' abcdefghijklmnopqrstuvwxyz ';                                 break;   &NBsp                     case ' 4 ':      ,                           $STR = ' Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ';                                 break;                         case ' 5 ':                                 $STR = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ123456 7890 ';                                 break;                         case ' 6 ':                                 $STR = ' abcdefghijklmnopqrstuvwxyz123456 7890 '; &nBsp                               break;                         default:         & nbsp                       $STR = ' ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKL mnopqrstuvwxyz1234567890 ';                                 break;                                 $CHECKST R= ';                 $len =strlen ($STR)-1;                 for ($i =0 $i < $length $i + +)           & nbsp     {                       //$num =rand (0, $len) Generates a random number between 0 and $len     &NBsp                   $num =mt_rand (0, $len);//Generate a random number between 0 and $len     &NBSP ;                   $CHECKSTR. = $str [$num];                                   &NB Sp      }                 return $checkstr;        }  /**     function name: Create_check_image ()         function: Generate a check code Pictures         parameters     Number: $checkcode: Checksum code string         return value: Back to this picture/        function Create_check_image ($checkcode)//Generate a         {        &NB Sp       $im =imagecreate (65,22)//Generate a picture                 $black =IMAGEC Olorallocate ($im, 0,0,0);//Background color           &NBSP     $white =imagecolorallocate ($im, 255,255,255);//foreground color                 $ Gray=imagecolorallocate ($im, 200,200,200);                 Imagefill ($im, 30,30, $gray)//in $im image coordinates 30,30 (0,0 in upper left corner of image) $gray Color execution area padding (that is, the same color as the 30, 30 points will be populated)                   imagestring ($im, 5,8,3,$ Checkcode, $white);//$white The string $checkcode to the 8, 3 coordinates of the image represented by $im (this is the upper-left corner of the string, the upper-left corner of the image is 0,0), 5 is the font size, the font can only be 1,2,3,4 or 5, Using built-in fonts                 for ($i =0 $i <120; $i + +)         &NBSP ;       {                        $randcolor =imag Ecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));                         Imagesetpixel ($im, Rand ()%70,rand ()%30, $randcolor)//on $im images with $randcolor color in (rand ()%70,rand ()%30) coordinates (imageTop left corner for 0,0) draw a dot                               & nbsp Header ("Content-type:image/png");                 imagepng ($im)//In PNG format to output the image to the browser or file         &NB Sp       Imagedestroy ($IM)//Destroy image $im        }}/* $randcode =new randcheckcode (); $checkstring = $randcode->get_code (5,7); $image = $randcode->create_check_image ($checkstring); Echo $image; * *  ?>  

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.