Php generates a verification code program with interference _ PHP Tutorial

Source: Internet
Author: User
Php generates a verification code program with interference. This section describes a verification generation program with high security. it can contain interference lines and other content, which can effectively prevent users from using the program to identify the verification code. The following code is used to copy the code. Here we introduce a verification generation program with high security. it can contain interference lines and other content, which effectively prevents users from using programs to identify the verification code.
The code is as follows:

/*
* Created on 2011-3-11
* Programmer: xiaoyao, QQ: 1045195056
Verify the input value and $ _ SESSION ['Check _ pic '] value
*/
Session_start ();
Function RandAscii ($ number) {// $ number generates numbers and letters
$ Arr = array ('0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9 ',
'A', 'B', 'C', 'D', 'e', 'e', 'F', 'G', 'H', 'I', 'J ', 'K', 'L', 'M', 'n', 'O', 'P', 'Q', 'R', 'S', 'T ', 'W', 'V', 'u', 'X', 'y', 'z ');
For ($ I = 1; $ I <= $ number; $ I ++)
{
$ Rand = $ rand. $ arr [rand (0, 35)];
}
Return $ rand;
}
$ Rand = RandAscii (4 );

$ _ SESSION ['Check _ pic '] = $ rand; // randomly generated four values are assigned to the session for verification.
$ X = 80;
$ Y = 24;
$ Im = imagecreatetruecolor ($ x, $ y); // create an image
$ Bg = imagecolorallocate ($ im, 255,255,255); // sets the color background.
Imagefill ($ im, 0, 0, $ bg );
$ Wh = imagecolorallocate ($ im, 255,255, 0 );
$ Gray = imagecolorallocate ($ im, 128,128,128 );
$ Yellow = imagecolorallocate ($ im, 255,255, 0 );
$ Red = imagecolorallocate ($ im, 0,255, 0 );
$ ForegroundArr = array (imagecolorallocate ($ im, rand (0, 20), rand (0, 20), rand (0, 20 )),
Imagecolorallocate ($ im, rand (0, 20), rand (0, 10), rand (245,255 )),
Imagecolorallocate ($ im, rand (245,255), rand (0, 20), rand (0, 10 )),
Imagecolorallocate ($ im, rand (245,255), rand (0, 20), rand (245,255 ))
); // Color array of words
// Draw a border
$ Border = imagecolorallocate ($ im, 133,153,193 );
Imagerectangle ($ im, 0, 0, $ x-1, $ y-1, $ border );

For ($ I = 0; $ I <10; $ I ++) {// draw interference lines, 10
Imageline ($ im, rand (0, 60), 2, rand (0, 60), 20, $ yellow );

}
For ($ j = 0; $ j <100; $ j ++ ){
Imagesetpixel ($ im, rand () % 76, rand () % 20, $ red );
}
// Imagestring ($ im, 6, 15, 8, $ rand, $ wh); // font size: 1-5
Imagettftext ($ im, 14, rand (30,-30), 5, rand (15, 18), $ foregroundArr [rand (0, 3)], 'C: WindowsFontsArial. ttf', $ rand [0]);
Imagettftext ($ im, 14, rand (50,-50), 24, rand (15, 18), $ foregroundArr [rand (0, 3)], 'C: WindowsFontsArial. ttf', $ rand [1]);
Imagettftext ($ im, 14, rand (50,-50), 43, rand (15, 18), $ foregroundArr [rand (0, 3)], 'C: WindowsFontsArial. ttf', $ rand [2]);
Imagettftext ($ im, 14, rand (30,-30), 62, rand (15, 18), $ foregroundArr [rand (0, 3)], 'C: WindowsFontsArial. ttf', $ rand [3]);

Header ("Content-type: image/jpeg"); // output image
Imagejpeg ($ im );
Imagedestroy ($ im );
?>

Call method

The code is as follows:
/*
* Created on 2011-3-11
* Programmer: xiaoyao, QQ: 1045195056
Verify the input value and $ _ SESSION ['Check _ pic '] value
*/
Session_start (); // enable session
If (isset ($ _ POST ['check'])
{
If ($ _ POST ['check'])
{
If ($ _ POST ['check'] ==_ _ SESSION ['Check _ pic '])
{
Echo "the verification code is correct". $ _ SESSION ['Check _ pic '];
}
Else
{
Echo "verification code error". $ _ SESSION ['Check _ pic '];
}
}
}
?>

Bytes. The code is 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.