Use PHP to implement the verification code function

Source: Internet
Author: User
Currently, many websites use verification codes to prevent users from automatically registering, logging on, and bumping through robots. The so-called verification code is to generate an image with a string of randomly generated numbers or symbols, and add some interference pixels to the image (preventing OCR). The user can identify the verification code information with the naked eye, enter the form to submit the website verification. a verification code can be used only after the verification is successful.

Currently, many websites have adopted
Verification code technology. The so-called verification code is to generate an image by generating a random string of numbers or symbols,
Add some interference pixels to the image (preventing OCR), and the user can identify the verification code information with the naked eye.
Enter the form and submit the website for verification. a function can be used only after the verification is successful.

Here we show how to write a PHP program to implement the verification code function:

Code 1:






/*
* Filename: authpage. php
* Author: huuworm
* Date: 2003-04-28
* @ Copyleft huuworm.org
*/

Srand (double) microtime () * 1000000 );

// Verify that the user input is consistent with the verification code
If (isset ($ HTTP_POST_VARS ['authinput'])
{
If (strcmp ($ HTTP_POST_VARS ['authnum'], $ HTTP_POST_VARS ['authinput']) = 0)
Echo "verification successful! ";
Else
Echo "verification failed! ";
}

// Generate a new four-digit integer verification code
While ($ authnum = rand () % 10000) <1000 );
?>

Code 2:






/*
* Filename: authimg. php
* Author: huuworm
* Date: 2003-04-28
* @ Copyleft huuworm.org
*/

// Generate a verification code Image
Header ("Content-type: image/PNG ");
Srand (double) microtime () * 1000000 );
$ Im = imagecreate (58,28 );
$ Black = ImageColorAllocate ($ im, 0, 0 );
$ White = ImageColorAllocate ($ im, 255,255,255 );
$ Gray = ImageColorAllocate ($ im, 200,200,200 );
Imagefill ($ im, 68, 30, $ gray );

// Print the four-digit integer verification code into the image
Imagestring ($ im, 5, 10, 8, $ HTTP_GET_VARS ['authnum'], $ black );

For ($ I = 0; $ I <50; $ I ++) // add interference pixels
{
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ black );
}

ImagePNG ($ im );
ImageDestroy ($ im );
?>


This program runs in the Apache 2.0.45 + PHP 4.3.1 environment.
The above is only a simple implementation of the verification code function, and does not consider commercial security issues. To enhance security and put this function into commercial applications, you can perform the following steps:

1. enable Session.
2. authnum is generated in authimg. php, md5sum is calculated, and stored in session.
3. after authpage. php calculates md5sum from authinput and compares it with authnum (md5sum) in the session to obtain the verification result.


Note: The author uses simple code to implement cool functions. However, the effect of adding interference pixels is not very good, you can take a look at the rain forum login verification code (http://ror.cn/perl/ut/user_login.cgi), even the second paragraph of the code slightly changed, A similar effect is generated.
The modified code is as follows:
/*
* Filename: authimg. php
* Author: huuworm
* Date: 2003-04-28
* @ Copyleft huuworm.org
*/
// Generate a verification code Image
Header ("Content-type: image/PNG ");
Srand (double) microtime () * 1000000 );
$ Im = imagecreate (62,20 );
$ Black = ImageColorAllocate ($ im, 0, 0 );
$ White = ImageColorAllocate ($ im, 255,255,255 );
$ Gray = ImageColorAllocate ($ im, 200,200,200 );
Imagefill ($ im, 68, 30, $ gray );
While ($ authnum = rand () % 100000) <10000 );
// Print the four-digit integer verification code into the image
Imagestring ($ im, 5, 10, 3, $ authnum, $ black );
For ($ I = 0; $ I <200; $ I ++) // add interference pixels
{
$ Randcolor = ImageColorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor );
}
ImagePNG ($ im );
ImageDestroy ($ im );
?>

If you are interested, you can try it on your own.

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.