


Implement verification code method in PHP development process_PHP tutorial
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)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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