Using PHP to implement verification code function _php Tutorial

Source: Internet
Author: User
Author: hutuworm Source: Confused and greedy temple
At present, many websites in order to prevent users to use the robot automatic registration, login, irrigation, have adopted a
Verification code technology. The so-called verification Code, is a series of randomly generated numbers or symbols, create a picture,
Images with some interference pixels (to prevent OCR), the user's visual identification of the verification code information, the output
into the form submission site validation, the validation is successful before you can use a feature.

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

Code One:

/*
* Filename:authpage.php
* Author:hutuworm
* date:2003-04-28
* @Copyleft hutuworm.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 "Verified success! ";
Else
echo "Verification failed! ";
}

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

Code two:

/*
* Filename:authimg.php
* Author:hutuworm
* date:2003-04-28
* @Copyleft hutuworm.org
*/

Generate a Captcha picture
Header ("Content-type:image/png");
Srand (Double) microtime () *1000000);
$im = Imagecreate (58,28);
$black = Imagecolorallocate ($im, 0,0,0);
$white = Imagecolorallocate ($im, 255,255,255);
$gray = Imagecolorallocate ($im, 200,200,200);
Imagefill ($im, 68,30, $gray);

Drawing a four-bit integer verification code into a picture
Imagestring ($im, 5, 8, $HTTP _get_vars[' Authnum '), $black);

for ($i =0; $i <50; $i + +)//Add interfering pixels
{
Imagesetpixel ($im, Rand ()%70, Rand ()%30, $black);
}

Imagepng ($im);
Imagedestroy ($im);
?>

This program runs through the Apache 2.0.45 + PHP 4.3.1 environment.

The above is just a simple implementation of the CAPTCHA function and does not take into account commercial security issues. If you want to enhance security and put this functionality into a commercial application, you can do so in the following few steps:

1. Enable session.
2. Authnum generated in the authimg.php, and calculated md5sum, deposited in session.
3. Authpage.php calculates the results of the validation by comparing the Authinput with the Authnum (md5sum) in the session md5sum.


Site Note: The author uses a simple code to achieve cool features. However, the effect of adding interference pixels is not very good, you can look at the sound of the Rain Forum login Check Code (HTTP://ROR.CN/PERL/UT/USER_LOGIN.CGI), I changed the second paragraph of the code a little bit, generated a similar effect.

The modified code is as follows:

/*
* Filename:authimg.php
* Author:hutuworm
* date:2003-04-28
* @Copyleft hutuworm.org
*/
Generate a Captcha picture
Header ("Content-type:image/png");
Srand (Double) microtime () *1000000);
$im = Imagecreate (62,20);
$black = Imagecolorallocate ($im, 0,0,0);
$white = Imagecolorallocate ($im, 255,255,255);
$gray = Imagecolorallocate ($im, 200,200,200);
Imagefill ($im, 68,30, $gray);
while (($authnum =rand ()%100000) <10000);
Drawing a four-bit integer verification code into a picture
Imagestring ($im, 5, 3, $authnum, $black);
for ($i =0; $i <200; $i + +)//Add interfering pixels
{
$randcolor = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($im, Rand ()%70, Rand ()%30, $randcolor);
}
Imagepng ($im);
Imagedestroy ($im);
?>


http://www.bkjia.com/PHPjc/314520.html www.bkjia.com true http://www.bkjia.com/PHPjc/314520.html techarticle Author: hutuworm Source: Confused and greedy temple at present, many websites in order to prevent users from using the robot automatic registration, login, irrigation, have adopted the Verification Code technology. The so-called verification code, is ...

  • 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.