Create a verification code based on PHP and a verification code based on PHP

Source: Internet
Author: User
Tags image identifier

Create a verification code based on PHP and a verification code based on PHP

The registration code is required to verify the validity of the current operator. In order to prevent the website from being maliciously registered by machines.

There are only a few steps to generate a verification code. First, we need to obtain a random string, create a canvas, and write the generated string to the canvas. We can also draw a snowflake line on the canvas, now post a piece of code to generate the verification code.

Source code:

<? Phpsession_start (); // enable the session // create a random code and save it in the session for ($ I = 0; $ I <4; $ I ++) {$ _ nmsg. = dechex (mt_rand ();} // save it to the session $ _ SESSION ['code'] = $ _ nmsg; // set the image length and height $ _ width = 75; $ _ height = 25; // create an image $ _ img = imagecreatetruecolor ($ _ width, $ _ height ); // white background $ _ white = imagecolorallocate ($ _ img, 255,255,255); // fill in imagefill ($ _ img, $ _ white) on the background ); // black border $ _ black = imagecolorallocate ($ _ img, 0); imagerectangle ($ _ img, $ _ width-1, $ _ heig Ht-1, $ _ black); // draw 5 lines for ($ I = 0; $ I <5; $ I ++) {$ _ rnd_color = imagecolorallocate ($ _ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); imageline ($ _ img, mt_rand (0, $ _ width), mt_rand (0, $ _ height), mt_rand (0, $ _ width), mt_rand (0, $ _ height), $ _ rnd_color );} // snowflake for ($ I = 0; $ I <10; $ I ++) {$ _ rnd_color = imagecolorallocate ($ _ img, mt_rand (200,255 ), mt_rand (200,255), mt_rand (200,255); imagestring ($ _ img, 1, mt_rand (1, $ _ width), mt_rand (1, $ _ Height), "*", $ _ rnd_color);} // output the verification code for ($ I = 0; $ I <strlen ($ _ SESSION ['code']); $ I ++) {imagestring ($ _ img, + $ I * 15, mt_rand (), $ _ SESSION ['code'] [$ I], $ _ blackr);} // output image header ('content-Type: image/png '); imagepng ($ _ img ); // destroy the image imagedestroy ($ _ img);?>

The following functions will be used in the Code:

Mt_rand-generate better random numbers
Int mt_rand ([int $ min], int $ max) many old libc random number generators have some uncertain and unknown features and are very slow. The rand () function of PHP uses the libc random number generator by default.

The mt_rand () function is informal to replace it. This function uses known features in Mersenne Twister as a random number generator, which can generate random values at an average speed four times faster than the rand () provided by libc.

Dechex-decimal conversion to hexadecimal return a string containing the hexadecimal representation of the given number parameter. The maximum value to be converted is 4294967295 in decimal format, and the result is "ffffffff ".

Imagecreatetruecolor-create a true color image
Resource imagecreatetruecolor (int $ x_size, int $ y_size)

Imagecreatetruecolor () returns an image identifier, representing a black image of x_size and y_size.

Imagecolorallocate-assign color to an image
Int imagecolorallocate (resource $ image, int $ red, int $ green, int $ blue)
Imagecolorallocate () returns an identifier representing a color consisting of a given RGB component. Red, green, and blue are the required colors respectively. These parameters are integers from 0 to 255 or hexadecimal values 0x00 to 0xFF. Imagecolorallocate () must be called to create the color of each image used in the image.

Imagefill-Region Filling
Bool imagefill (resource $ image, int $ x, int $ y, int $ color)
Imagefill () is filled with the color execution area (that is, with x, y points are in the same color and adjacent points are filled ).

Imagerectangle-draw a rectangle
Bool imagerectangle (resource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ col)
Imagerectangle () uses the col color to draw a rectangle in the image. The coordinates in the upper left corner are x1 and y1, and those in the lower right corner are x2 and y2. The coordinates in the upper left corner of the image are 0 and 0.

Imageline-draw a line segment
Bool imageline (resource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ color)
Imageline () uses color to draw a line segment from the coordinates x1, y1 to x2, y2 (0 in the upper left corner of the image.

Imagestring-draw a line of strings horizontally
Bool imagestring (resource $ image, int $ font, int $ x, int $ y, string $ s, int $ col)
Imagestring () uses the col color to draw string s to the x and y coordinates of the image (this is the coordinate of the upper left corner of the string, and the upper left corner of the entire image is 0, 0 ). If the font is 1, 2, 3, 4, or 5, the built-in font is used.

Imagepng-output the image to a browser or file in PNG format
Imagepng () outputs the GD image stream (image) in PNG format to the standard output (usually in a browser), or outputs the file name to the file if filename is used.

Imagedestroy-destroy an image

Imagedestroy () releases the memory associated with the image.

Saving the source code as code. php is a php file. How should we use it?

Imagepng has output the PHP file as a png file.

You can call it directly.

If you want to use the verification code, remember to enable the session.

<?phpsession_start();echo $_SESSION['code'];?>

I hope this article will help you with PHP programming.

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.