I have recently learned how to get started with php. Now I just got started, so I don't know much about it. I just learned from the basics and won't check it online. I will keep it in this magic weapon, just as today I met the rand () function. I thought about how to use it. I finally remembered the verification code, digital verification code, letter verification code, and Chinese verification code, but I couldn't, what should I do? search online, read other people's code, don't understand, watch videos, listen to the teacher, and take notes of the functions encountered in them, generally, the random verification codes on the Web page are surrounded by a certain number of boxes, which seem to be in the background of images.
After reading it, I think that although I have encountered many problems, I believe that as long as I am down to earth, I will definitely learn. Now I want to make a summary. I may write a mess myself, but I believe it will happen one day.
1. random Number Generation -- Create Image -- write random number into image -- add interference value (point, line) to image -- keep in session -- reference in form;
Random Functions: rand (int min, int max); tens of thousands of changes, I read a lot of code for generating random numbers on the Internet, including random numbers and letters, random numbers (arrays), and so on; can't do without rand ();
The Code is as follows (I hope you will not be surprised to copy the code online.
First:
$ Authnum = '';
$ Ychar = ", 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); // delimiter Function
For ($ I = 0; $ I <4; $ I ++ ){
$ Randnum = rand (0, 35 );
$ Authnum. = $ list [$ randnum]; // output as an array
Second:
Private function createCheckCode ()
{
For (I = 0; I <this-> codeNum; I ++)
{
Number = rand (0, 2 );
Switch (number)
{
Case 0: rand_number = rand (48, 57); break; // number
Case 1: rand_number = rand (); break; // uppercase letter
Case 2: rand_number = rand (97,122); break; // lowercase letter
}
$ Asc = sprintf ("% c", rand_number );
$ Asc_number = asc_number.asc;
}
Return asc_number;
}
Third:
Srand (microtime () * 100000); // equivalent to a timer
$ String = "abcdefghigklmnopqrstuvwxyz123456789 ";
For ($ I = 0; $ I <4; $ I ++)
{
$ New_number. = $ string [rand (0, strlen ($ string)-1)]; // an array is generated immediately.
}
Fourth:
For ($ I = 0; $ I <4; $ I ++)
{
$ Rand. = dechex (rand (); // convert decimal to hexadecimal
}
GD library: (provides IPI for a series of image processing functions to generate image processing images)
Enable GD library in php: in the php. ini configuration file, remove ";" from "; extension = php_gd2.dll";
Some GD library functions:
1. imagecreatetruecolor (int x_size, int Y_size) create a true color image
2. imagecolorallocate (resource image, int red, int green, int blue) allocates color to an image with three primary colors.
3. imagestring (resource, font, int x, int y, content, color) plotting function
4. header ("Content-type: image/jpeg") output function www.2cto.com
The php header defines the action of the header, and php5 supports three types:
1, Content-type: xxxx/yyyy
2, Location: xxxx: yyyy/zzzz
3, Status: nnn xxxxxx
Xxxx/yyyy indicates the content file type
For example, image/gif
Image/jpeg
Image/png
Imagejpeg (), imagegif (), imagepang ()
5. iamgeline (resource image, int x1, int y1, int x2, int y2, int color); line function, (int x, int y) Start Coordinate
6. imagesetpixel (resource image, int x, int y, int color) Point Function
7. imagettftext (resource image, float size, float angle, int x, int y, int color, string fontfile, string text) with font Writing Function
8. iconv ("gb2312", "UTF-8", "string"); // you must first convert the text into a UTF-8 php verification code and insert it into Chinese.
For the code, see PHP Verification Code production (below)
From ms. RMB