Php entry-level PHP Verification Code Program

Source: Internet
Author: User

To generate a verification code, we need to use php graphics processing functions, such as imagecreate, imagepng, and header. Let's take a look at a simple example.

Instance

The Code is as follows: Copy code

<? Php
Session_start ();

$ Im = imagecreate (80, 30); // create an image
$ Color = imagecolorallocate ($ im, rand (150,200), rand (150,200), rand (150,200); // set the image background
$ Str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; // generate a random string
For ($ I = 0; $ I <5; $ I ++ ){
$ Code. = $ str [rand (0, (strlen ($ str)-1)];
}
$ _ SESSION ['code'] = $ code;
For ($ a = 0; $ a <5; $ a ++) {// write strings to image resources
$ X = $ a * 10 + 15;
$ Y = rand (5, 10); // www. bKjia. c0m
Imagechar ($ im, 5, $ x, $ y, $ code {$ a}, imagecolorallocate ($ im, 0, 0 ));
}
Header ("Content-type: image/png"); // outputs image resources
Imagepng ($ im );
?>

Example 2

The Code is as follows: Copy code

<? Php
If (! Isset ($ _ SESSION) {// checks whether the session is enabled
Session_start (); // enable session
}
$ Width = 70; // width of the canvas
$ Height = 25; // canvas height
$ Length = 4; // verification code length
$ Code = getcode ($ length); // obtain a random string
$ _ SESSION ['verfycode'] = $ code;

$ Img = imagecreate ($ width, $ height );
$ Bgcolor = imagecolorallocate ($ img, 240,240,240 );
$ Rectangelcolor = imagecolorallocate ($ img, 150,150,150 );
Imagerectangle ($ img, $ width-1, $ height-1, $ rectangelcolor); // draw a border
For ($ I = 0; $ I <$ length; $ I ++) {// circular writing
$ Codecolor = imagecolorallocate ($ img, mt_rand (50,200), mt_rand (50,128), mt_rand (50,200 ));
$ Angle = rand (-20, 20 );
$ Charx = $ I * 15 + 8;
$ Chary = ($ height + 14)/2 + rand (-1, 1 );
Imagettftext ($ img, 15, $ angle, $ charx, $ chary, $ codecolor, 'c: WINDOWSFontsSIMKAI. ttf ',
$ Code [$ I]);
}
For ($ I = 0; $ I <20; $ I ++) {// looping
$ Linecolor = imagecolorallocate ($ img, mt_rand (0,250), mt_rand (0,250), mt_rand (0,250 ));
$ Linex = mt_rand (1, $ width-1 );
$ Liney = mt_rand (1, $ height-1 );
Imageline ($ img, $ linex, $ liney, $ linex + mt_rand ()-2, $ liney + mt_rand ()-2, $ linecolor );
}
For ($ I = 0; $ I <100; $ I ++) {// recurrence point
$ Pointcolor = imagecolorallocate ($ img, mt_rand (0,250), mt_rand (0,250), mt_rand (0,250 ));
Imagesetpixel ($ img, mt_rand (1, $ width-1), mt_rand (1, $ height-1), $ pointcolor );
}
Function getcode ($ length) {// generate a php Random Number
$ Pattern = '1234567890abcdefghijklomnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '; // character pool
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Key. = $ pattern {mt_rand (0, 35 )};
}
Return $ key;

}
Ob_clean ();
Header ('content-type: image/png ');
Imagepng ($ img );
?>

As follows:

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.