Using PHP to implement verification code function _php Foundation

Source: Internet
Author: User
Tags rand

Author: hutuworm Source: Confused greedy temple
At present, many websites in order to prevent users to use robot automatic registration, login, irrigation, have adopted
Verification code technology. The so-called verification code, is a string of randomly generated numbers or symbols to generate a picture,
The image with a number of interference pixels (to prevent OCR), by the user's eyes to identify the authentication code information, the loss
Submit a Web site validation to the form and verify that you are successful before you can use a feature.

We show here how to write PHP program to implement the verification code function:

Code One:

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

Srand (Double) microtime () *1000000);

Verify that user input is consistent with the authentication code
if (isset ($HTTP _post_vars[' authinput '))
{
if (strcmp ($HTTP _post_vars[' Authnum '), $HTTP _post_vars[' authinput ') ==0)
echo "Validation successful! ";
Else
echo "Validation failed! ";
}

To generate a new four-bit integer verification code
while (($authnum =rand ()%10000) <1000);
?>
<form action=authpage.php method=post>
<table>
Please enter the code: <input type=text name=authinput style= "width:80px" ><br>
<input type=submit name= "Authentication" value= "Submit verification Code" >
<input Type=hidden name=authnum value=<? echo $authnum;?>>
>
</table>
</form>

Code two:

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

Generate 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);

To draw 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 interference pixels
{
Imagesetpixel ($im, Rand ()%70, Rand ()%30, $black);
}

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

This program is run in the Apache 2.0.45 + PHP 4.3.1 environment.

This is just a simple implementation of the verification Code feature and does not take into account commercial security issues. If you want to enhance security and put this functionality into commercial use, you can do this by following these steps:

1. Enable session.
2. Authnum is generated in authimg.php, and the md5sum is calculated and stored in session.
3. Authpage.php calculates the md5sum of the authinput, and results the validation with the Authnum (Md5sum) in session.


This site note: The author uses a simple code to achieve a cool function. However, in addition to interfere with the pixel effect is not too good, you can look at the Rain forum login when the parity code (HTTP://ROR.CN/PERL/UT/USER_LOGIN.CGI), I changed the second paragraph of the code slightly, resulting in its similar effect.

The modified code is as follows:

<?php
/*
* Filename:authimg.php
* Author:hutuworm
* date:2003-04-28
* @Copyleft hutuworm.org
*/
Generate 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);
To draw a four-bit integer verification code into a picture
Imagestring ($im, 5, 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);
?>


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.