A practical _php technique for PHP verification code function

Source: Internet
Author: User
Tags rand

This example for you to share a few sections of PHP verification code functions, are very practical, the specific contents are as follows

Code Snippet One: Simple PHP Verification code function

<?php function code () {$im = Imagecreatetruecolor (100, 40);
 
 $black = imagecolorallocate ($im, 0, 0, 0);
 
 
 
 $white = Imagecolorallocate ($im, 255, 255, 255);
 
 Imagefill ($im, 0,0, $white);
 
 $strarr =array_merge (Range (0,9), Range (a,z), Range (a,z));
 
 Shuffle ($strarr);
 
 $str =join (Array_slice ($strarr, 0,4));
 
 $MM =rand (0,40);
 
 $AAA =rand (0,0);
 
 $BBB =rand (0,255);
 
 $CCC =rand (0,255);
 
 $color 1=imagecolorallocate ($im, $AAA, $BBB, $CCC);
 
 Imagestring ($im, 5, $mm, $str, $color 1);
 
   For ($i =0 $i <700; $i + +) {$aa =rand (0,255);
 
   $BB =rand (0,255);
 
 $CC =rand (0,255);
 
   $color =imagecolorallocate ($im, $aa, $BB, $CC);
 
   $a =rand (0,100);
 
   $b =rand (0,40);
 
 Imagesetpixel ($im, $a, $b, $color);
 
   For ($t =0 $t <3; $t + +) {$a 1=rand (0,255);
 
   $b 1=rand (0,255);
 
 $c 1=rand (0,255);
 
   $color 2=imagecolorallocate ($im, $a 1, $b 1, $c 1);
 
   $a 2=rand (0,100);
 
   $b 2=rand (0,40);
 
   $a 3=rand (0,100);
 
   $b 3=rand (0,40);
 
 Imageline ($im, $a 2, $b 2, $b 3, $b 3, $color 2);
 
} 
 
 Header ("Content-type:image/png");
 
 Imagepng ($im);
 
} code ();

 ?>

Code Snippet two: PHP validation code function

function _code ($_code_length = 4, $_width = +, $_height =) {for ($i =0; $i <$_code_length; $i + +) {$_nmsg. = Dechex (M
 T_rand (0,15));

 } $_session["Code" = $_nmsg;

 $_img = Imagecreatetruecolor ($_width, $_height);

 $_white = Imagecolorallocate ($_img, 250, 250, 250);

 Imagefill ($_img, 0, 0, $_white);

 $_gray = Imagecolorallocate ($_img, 196, 196, 196);

 Imagerectangle ($_img, 0, 0, $_width-1, $_height-1, $_gray); For ($i =0 $i < 6; $i + +) {$_md_color = Imagecolorallocate ($_img, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,25
  5));
 Imageline ($_img, Mt_rand (0,$_width), Mt_rand (0, $_height), Mt_rand (0,$_width), Mt_rand (0, $_height), $_md_color); For ($i =0 $i < $i + +) {$_md_color = Imagecolorallocate ($_img, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (2
  00,255));
 Imagestring ($_img, 1, Mt_rand (1,$_width-5), Mt_rand (1, $_height-5), "*", $_md_color); For ($i =0 $i < $_code_length $i + +) {$_md_color = Imagecolorallocate ($_img, Mt_rand (0,102), Mt_rand (0,102), Mt_rand (0,102)); Imagestring ($_img, 5, $i * $_width/$_code_length+ Mt_rand (1), Mt_rand (1, $_HEIGHT/2), $_session["code" [$i], $_md_
 color);

 Header ("Content-type:image/png");

 Imagepng ($_IMG);
Imagedestroy ($_IMG);

 }

Code Snippet three: PHP Picture Verification code function

function of the/*@ captcha () function: Generate Authenticode * @ Customizable parameters: * @ $width picture width, default $high height, default of * @ $num Verification code number, default 4 * @ $line _num randomly drawn lines Number, default x @ $snow _num random snowflakes, default/function Captcha ($width =80, $high =25, $num =4, $line _num=10, $snow _num=50) {header (' C
 Ontent-type:image/png ');
 Session_Start (); Generate a random number + letter for ($a = 0; $a < $num; $a + +) {$code. = Dechex (Mt_rand (0));//dechex-decimal to hexadecimal}//Save generated CAPTCHA in Sessio
 N Super Global Array $_session[' captcha ' = $code;
 Create Canvas $img = Imagecreatetruecolor ($width, $high);
 The fill background color is white $backcolor = imagecolorallocate ($img, ' 255 ', ' 255 ', ' 255 ');
 Imagefill ($img, ' 0 ', ' 0 ', $backcolor);
 Add a black border $bordercolor = imagecolorallocate ($img, 0, 0, 0);
 Imagerectangle ($img, 0, 0, $width-1, $high-1, $bordercolor); Random draw lines for ($i =0; $i < $line _num; $i + +) {imageline ($img, Mt_rand (0, $width *0.1), Mt_rand (0, $high), Mt_rand ($width *0.9
 , $width), Mt_rand (0, $high), Imagecolorallocate ($img, Mt_rand (255), Mt_rand (255), Mt_rand (150, 255)); ///randomly hit snowflake for ($i =0; $i < $snow _num $i + +) {Imagechar ($img, 1, mt_rand (0, $width), Mt_rand (0, $high), ' * ', imagecolorallocate ($img, Mt_rand (200
 , 255), Mt_rand (MB, 255), Mt_rand (200, 255)); //Draw Verification code for ($b = 0; $b < strlen ($_session[' captcha ')) $b + +) {Imagechar ($img, 5, $b * $width/$num +mt_rand (5,10), Mt_ra nd (2, $high/2), $_session[' Captcha '] [$b], Imagecolorallocate ($img, Mt_rand, Mt_rand (0, 100))
 );
 Ob_clean ();//Empty output buffer imagepng ($img);
Imagedestroy ($IMG);

 }

Above is a three-paragraph reference to the higher PHP verification code function, I hope to learn from the PHP program to help.

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.