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

Implement verification code method in PHP development process_PHP tutorial

Jul 13, 2016 pm 05:33 PM
php about exist accomplish develop article of code random verify


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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles