Home  >  Article  >  Backend Development  >  Implement verification code method in PHP development process_PHP tutorial

Implement verification code method in PHP development process_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:33:23760browse


I read some articles about verification codes some time ago, which is to generate a picture from a string of randomly generated numbers or symbols, add some interference pixels to the picture (to prevent OCR), and let the user identify the verification code with the naked eye. Information, enter the form to submit for website verification, and a certain function can only be used after successful verification.
 
 There is an article that briefly introduces the implementation method, as follows:
 
 Code 1:
 (as the current mainstream development language)
 
 /*
 
 * Filename: authpage.php(as the current mainstream development language)
 
 * Author: hutuworm

* Date: 2003-04-28

* @Copyleft hutuworm.org

 
 //Verify whether the user input is consistent with the verification code
 
 if(isset($HTTP_POST_VARS[auhinput]))
 
 {
 
 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:

 (as the current mainstream development language)
  / * 
 * Filename: authimg.php
(as the current mainstream development language)

  * Author: hutuworm 
 * Date: 2003-04 -28
 
 * @Copyleft hutuworm.org
 
 */
 
 //Generate verification code image
 
 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);
 
 //Draw 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 has basically implemented the verification code generation and verification functions, but the author of the article does not know why the content of the verification code is displayed in the form, so In this case, it only restricts users from entering the verification code, but does not have any preventive effect on malicious programs. It can be said that it is to make things difficult for others, rather than to prevent attacks.
 
But fortunately, according to the original author's idea, we can save the verification string in the session, so that it has a certain degree of security.
 
 The code is as follows:
 //file:authform.php(as the current mainstream development language)
 
 
 
 
 
 (as the current mainstream development language)
 
 /*
 
 *" Filename: authimg.php (As the current mainstream development language)
 
 */
 
 Header("Content-type:image/PNG");
 
 session_start();
 
 $auth_num="";
 
 session_register(auth_num);
 
 $im=imagecreate(63,20);
 
 srand((double)microtime ()*1000000);
 
$auth_num_k=md5(rand(0,9999));

$auth_num=substr($auth_num_k,17,5);

$black=ImageColorAllocate($im,0,0,0);

$white=ImageColorAllocate($im,255,255,255);

$gray=ImageColorAllocate($im,200,200,200);
 
 //ImageFill($im,63,20,$black);//I don’t know why this line went wrong on my company’s server, please change the space ok
 
 imagestring($im, 5,10,3,$auth_num,$gray);
 
for($i=0;$i<200;$i++)
 
 {
 
 $randcolor= ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
 
imagesetpixel($im,rand()%70,rand()%30,$randcolor);

 }

ImagePNG($im);

ImageDestroy($im);

?>

(As the current mainstream development language)
 
 /*
 
 * Filename:authpage.php(As the current mainstream development language)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508612.htmlTechArticleI read some articles about verification code some time ago, which is to generate a string of randomly generated numbers or symbols. A picture, some interfering pixels are added to the picture (to prevent OCR), and the user fleshes it out...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn