- $authnum = ";
- $ychar = "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";
- $list =explode (",", $ychar);//Split function
- for ($i =0; $i <4; $i + +) {
- $randnum =rand (0,35);
- $authnum. = $list [$randnum];//output as an array
Copy CodeMethod Two, defined as a private function.
- Private Function Createcheckcode ()
- {
- For (i=0;i codenum;i++)
- {
- Number = rand (0,2);
- Switch (number)
- {
- Case 0:rand_number = rand (48,57); break;//Digital
- Case 1:rand_number = rand (65,90); break;//Capital Letter
- Case 2:rand_number = rand (97,122); break;//lowercase Letter
- }
- $ASC = sprintf ("%c", Rand_number);
- $ASC _number = ASC_NUMBER.ASC;
- }
- return asc_number;
- }
Copy CodeMethod Three, use random seed to generate PHP verification code.
- Srand (Microtime () *100000);//equivalent to a timer
- $string = "abcdefghigklmnopqrstuvwxyz123456789";
- for ($i =0; $i <4; $i + +)
- {
- $new _number.= $string [Rand (0,strlen ($string)-1)];//immediately produces an array
- }
Copy CodeMethod Four,
- for ($i =0; $i <4; $i + +)
- {
- $rand. =dechex (rand (1,15));//convert decimal to hexadecimal
- }
Copy CodeNext enter the focus of this article: PHP GD Library: (provides a series of image processing functions of IPI, generated image processing image) enable PHP in the GD library: php.ini configuration file, remove the "; Extension=php_gd2.dll" in ";"; Part of the GD library functions: 1.imagecreatetruecolor (int x_size,int y_size) New true Color Image 2.imagecolorallocate (Resource Image,int Red,int Green,int blue) assigns a color to an image, the primary 3.imagestring (Resource,font,int x,int y,content,color) Drawing function 4.header ("Content-type: Image/jpeg ") output function the header of PHP is defined as the action of the head, the type in the PHP5 support 3:1,content-type:xxxx/yyyy 2,location:xxxx:yyyy/zzzz 3,status:nnn XXXXXX xxxx/yyyy represents the type of content file such as: Image/gif image/jpeg image/png imagejpeg (), Imagegif (), Imagepang () 5.iamgeline (Resource Image,int x1,int y1,int x2,int y2,int color); Draw line function, (int x,int y) Start coordinate 6.imagesetpixel (resource image,int x,int y,int color) Draw point function 7.imagettftext (Resource image,float Size,float angle,int x,int y,int color,string fontfile,string text) with font write function 8.iconv ("gb2312", "Utf-8", "string");// First of all to convert text into utf-8 format PHP code to insert Chinese method, here can refer to the next PHP Chinese code generation method. Example 1, randomly generated numbers, letters of code:
- che.php
- Session_Start ();
- for ($i =0; $i <4; $i + +)
- {
- $rand. =dechex (rand (1,15));
- }
- $_session[' Check_num ']= $rand;
- $image =imagecreatetruecolor (50,30);
- $BG =imagecolorallocate ($im, 0,0,0);//When using the palette for the first time, the background color
- $te =imagecolorallocate ($im, 255,255,255);
- Imagestring ($image, 6,rand (0,20), Rand (0,2), $rand, $te);
- Ob_clean (); the image "Http://localhost/**.php" appears in the//php Web page because it has to generate a verification code and cannot be displayed because of its own error
- Header ("Content-type:image/jpeg"); Imagejpeg ($image);
- ?>
Copy CodeExample 2, draw a line of interference code for the picture:
- for ($i =0; $i <8; $i + +)//Draw more than one line
- {
- $CG =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//produce random colors
- Imageline ($im, Rand (10,40), 0,rand (10,40), $CG);
- }
Copy CodeExample 3, the code for the image to draw a disturbance point:
- for ($i =0; $i <80; $i + +)//Draw multiple points
- {
- Imagesetpixel ($im, Rand (0,40), Rand (0,20), $CG);
- }
Copy CodeExample 4, write the text to the image code:
$str =array (' i ', ' I ', ' pro ', ' Pro ');//store displayed Kanji
- for ($i =0; $i <4; $i + +)
- {
- $sss. = $str [Rand (0,3)];//random display of Chinese characters
- }
$str =iconv ("gb2312", "Utf-8", $str); Chinese character coding transformation, I don't seem to need
- Imagettftext ($im, 10,0,rand (5,60), Rand (5,60), $te, "Simhei.ttf", $sss);//
Copy Code0: The tilt of the font, "Simhei.ttf": Font style, usually placed in the root directory; This completes the PHP picture verification Code of the entire production process, script small hope this article to use the GD library to generate PHP verification code, have some help. |