As I encountered today, the function rand (); Think of something to do with it, right? Finally remembered the verification code, the digital verification Code, the letter verification Code, the Chinese verification code, but oneself does not have, how to do it, the Internet searches, sees other people's code, does not understand, sees the video, listens to the teacher to speak, will have encountered the function, Noteworthy places take notes, usually see the general Web page of the random verification code is surrounded by a certain box, it seems that the picture is the background. After watching, their own side knocking, although encountered a lot of problems, but I believe that as long as their down-to-earth, must learn. Now want to do a summary, I may write a very messy, but I believe that one day will be achieved. 1. The random number that produces a number--"Create picture--" random number into the picture--"In the picture add the disturbing value (dots, lines)-" Keep in session-"in the form form of reference; random function: rand (int min,int max); same, I have seen many of the online generation of random numbers of code, there are numbers and letters random number, Chinese random number (array) and so on, are inseparable from rand (), the code is as follows (some Internet copy, I hope you don't mind the first kind:
Copy Code code as follows:
$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
The second type:
Copy Code code as follows:
Private Function Createcheckcode ()
{
for (i=0;i<this->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;//uppercase letters
Case 2:rand_number = rand (97,122); break;//lowercase Letters
}
$ASC = sprintf ("%c", Rand_number);
$ASC _number = ASC_NUMBER.ASC;
}
return asc_number;
}
The third type:
Copy Code code as follows:
Srand (Microtime () *100000);/equal to Timer
$string = "abcdefghigklmnopqrstuvwxyz123456789";
For ($i =0 $i <4; $i + +)
{
$new _number.= $string [Rand (0,strlen ($string)-1)];//immediately produces an array
}
The fourth kind:
Copy Code code as follows:
For ($i =0 $i <4; $i + +)
{
$rand. =dechex (rand (1,15));//convert decimal to hexadecimal
}
GD Library: (provides a series of IPI of image processing function, generate picture processing picture)
Enable the GD library in PHP: php.ini configuration file, remove "; Extension=php_gd2.dll" in ";";
Introduction to some 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 color to an image, three primary colors
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 header, in PHP5 support 3 type: 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, int y1,int x2,int y2,int color); Draw line function, (int x,int y) starting coordinates 6.imagesetpixel (resource image,int x,int y,int color) dot function 7.imagettftext (resource image,float Size,float angle,int x,int y,int color,string text) with font write function 8.iconv ("fontfile,string", "gb2312", "string"); First, you convert the text into the Utf-8 format PHP code to insert the Chinese method.
Randomly generated numbers, letters of code:
Copy Code code as follows:
<?php
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);//The first time you use a palette, the background color
$te =imagecolorallocate ($im, 255,255,255);
Imagestring ($image, 6,rand (0,20), Rand (0,2), $rand, $te);
Ob_clean ()//php the image "http://localhost/**.php" in the Web page because the validation code is to be generated cannot be displayed because of its own error
Header ("Content-type:image/jpeg"); Imagejpeg ($image);
?>
Draw the interference line code for the picture:
Copy Code code as follows:
For ($i =0 $i <8; $i + +)//Draw more than one line
{
$CG =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//produce a random color
Imageline ($im, Rand (10,40), 0,rand (10,40), $CG);
}
To draw a disturbing point code for a picture:
Copy Code code as follows:
For ($i =0 $i <80; $i + +)//Draw multiple points
{
Imagesetpixel ($im, Rand (0,40), Rand (0,20), $CG);
}
Write text to the picture code:
Copy Code code as follows:
$str =array (' i ', ' I ', ' pro ', ' Pro ');//store displayed Kanji
For ($i =0 $i <4; $i + +)
{
$sss. = $str [Rand (0,3)];//randomly displays Chinese characters
}
$str =iconv ("gb2312", "Utf-8", $str); Encoding transformation, I don't seem to need
Imagettftext ($im, 10,0,rand (5,60), Rand (5,60), $te, "Simhei.ttf", $sss);
0: The tilt of the font, "Simhei.ttf": Font style, generally placed in the root directory;