A simple php Verification Code Program-PHP source code

Source: Internet
Author: User
This article introduces the code generated by the Verification Code Program in php, the method of calling, and whether the verification code program entered by the user is correct. This article introduces the code generated by the Verification Code Program in php, the method of calling, and whether the verification code program entered by the user is correct.

Script ec (2); script

Create_code.php code

The Code is as follows:
Session_start ();
// Generate a verification code Image
Header ("Content-type: image/png ");
// All numbers
$ Str = "1, 2, 3, 4, 5, 6, 7, 8, 9, a, B, c, d, f, g"; // you can add or delete the characters to be displayed.
$ List = explode (",", $ str );
$ Cmax = count ($ list)-1;
$ VerifyCode = '';
For ($ I = 0; $ I <5; $ I ++ ){
$ Randnum = mt_rand (0, $ cmax );
$ VerifyCode. = $ list [$ randnum]; // extract the characters and combine them into the verification code characters.
}
$ _ SESSION ['code'] = $ verifyCode; // put the characters in the SESSION

$ Im = imagecreate (58,28); // generate an image
$ Black = imagecolorallocate ($ im, 0); // set the color of the Bar and the following three items
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Gray = imagecolorallocate ($ im, 200,200,200 );
$ Red = imagecolorallocate ($ im, 255, 0, 0 );
Imagefill ($ im, $ white); // fill the color of the image

// Print the verification code into the image
Imagestring ($ im, 5, 10, 8, $ verifyCode, $ black); // write the verification code to the image.

For ($ I = 0; $ I <50; $ I ++) // Add interference pixels
{
Imagesetpixel ($ im, rand () p, rand () 0, $ black); // Add point interferon
Imagesetpixel ($ im, rand () p, rand () 0, $ red );
Imagesetpixel ($ im, rand () p, rand () 0, $ gray );
// Imagearc ($ im, rand () p, rand () p, 20, 20, 75,170, $ black); // Add an arc-like interferon
// Imageline ($ im, rand () p, $ red); // Add line-like interferon
}
Imagepng ($ im );
Imagedestroy ($ im );
?>

Html code

Demo.html

The Code is as follows:








Script
Function create_code (){
Document. getElementByIdx_x ('code'). src = 'create _ code. php? '+ Math. random () * 10000;
}
Script

// Process and determine whether the input is correct
Act. php

The Code is as follows:

Session_start ();

If ($ _ POST ['code'] ==_ _ SESSION ['code']) {
Echo 'OK ';
} Else {
Echo 'no ';
}
?>

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.