Home Backend Development PHP Tutorial PHP simple verification code generation example_PHP tutorial

PHP simple verification code generation example_PHP tutorial

Jul 13, 2016 am 10:47 AM
php use Can Example password tool submit Violence generate user of code Simple repeat prevent verify

Verification codes can prevent some users from repeatedly submitting to guess passwords or using brute force tools to guess passwords. After we added verification codes, it made it more difficult for them. Below I provide a session-based verification code program.


When logging in and registering on a website, verification codes are often used to prevent others from using mechanical violence to register or log in. Adding the verification code makes the website much safer to a certain extent. The following is a relatively simple verification code generation, which also provides session assignment.

The code is as follows
 代码如下 复制代码

session_start();
header(“Content-type: image/png”);
//创建真彩色白纸
$im = @imagecreatetruecolor(50, 20) or die(“建立图像失败”);
//获取背景颜色
$background_color = imagecolorallocate($im, 255, 255, 255);
//填充背景颜色(这个东西类似油桶)
imagefill($im,0,0,$background_color);
//获取边框颜色
$border_color = imagecolorallocate($im,200,200,200);
//画矩形,边框颜色200,200,200
imagerectangle($im,0,0,49,19,$border_color);

//逐行炫耀背景,全屏用1或0
for($i=2;$i<18;$i++){
//获取随机淡色
$line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
//画线
imageline($im,2,$i,47,$i,$line_color);
}

//设置字体大小
$font_size=12;

//设置印上去的文字
$Str[0] = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
$Str[1] = “abcdefghijklmnopqrstuvwxyz”;
$Str[2] = “01234567891234567890123456″;

//获取第1个随机文字
$imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
$imstr[0]["x"] = rand(2,5);
$imstr[0]["y"] = rand(1,4);

//获取第2个随机文字
$imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];
$imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);
$imstr[1]["y"] = rand(1,3);

//获取第3个随机文字
$imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
$imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);
$imstr[2]["y"] = rand(1,4);

//获取第4个随机文字
$imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
$imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);
$imstr[3]["y"] = rand(1,3);

//将显示的数组赋值给session
$_SESSION['CODE'] = $imstr[0]["s"].$imstr[1]["s"].$imstr[2]["s"].$imstr[3]["s"];

//写入随机字串
for($i=0;$i<4;$i++){
$text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));
imagechar($im,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color);
}
//显示图片
imagepng($im);
//销毁图片
imagedestroy($im);
?>

Copy code
session_start();

header(“Content-type: image/png”);

//Create true color white paper //Get the background color $background_color = imagecolorallocate($im, 255, 255, 255); //Fill the background color (this thing is similar to an oil drum) imagefill($im,0,0,$background_color); //Get the border color $border_color = imagecolorallocate($im,200,200,200); //Draw a rectangle, border color 200,200,200 imagerectangle($im,0,0,49,19,$border_color); //Show off the background line by line, use 1 or 0 for full screen for($i=2;$i<18;$i++){
//Get random light color

$line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
//Draw line
imageline($im,2,$i,47,$i,$line_color);
}<🎜> <🎜>//Set font size<🎜> $font_size=12;<🎜> <🎜>//Set the printed text<🎜> $Str[0] = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;<🎜> $Str[1] = “abcdefghijklmnopqrstuvwxyz”;<🎜> $Str[2] = “01234567891234567890123456″;<🎜> <🎜>//Get the first random text<🎜> $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];<🎜> $imstr[0]["x"] = rand(2,5);<🎜> $imstr[0]["y"] = rand(1,4);<🎜> <🎜>//Get the second random text<🎜> $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];<🎜> $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);<🎜> $imstr[1]["y"] = rand(1,3);<🎜> <🎜>//Get the 3rd random text<🎜> $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];<🎜> $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);<🎜> $imstr[2]["y"] = rand(1,4);<🎜> <🎜>//Get the 4th random text<🎜> $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];<🎜> $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);<🎜> $imstr[3]["y"] = rand(1,3);<🎜> <🎜>//Assign the displayed array to session<🎜> $_SESSION['CODE'] = $imstr[0]["s"].$imstr[1]["s"].$imstr[2]["s"].$imstr[3]["s" ];<🎜> <🎜>//Write random string<🎜> for($i=0;$i<4;$i++){<🎜> $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));<🎜> imagechar($im,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color);<🎜 > }<🎜> //Display picture<🎜> imagepng($im);<🎜> //Destroy the picture<🎜> imagedestroy($im);<🎜> ?> http://www.bkjia.com/PHPjc/632840.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632840.htmlTechArticleVerification code can prevent some users from repeatedly submitting to guess passwords or using brute force tools to guess passwords. We have added verification codes After that, it added a lot of difficulty to them. Below I provide a model based on...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
What kind of software is a digital currency app? Top 10 Apps for Digital Currencies in the World What kind of software is a digital currency app? Top 10 Apps for Digital Currencies in the World Apr 30, 2025 pm 07:06 PM

With the popularization and development of digital currency, more and more people are beginning to pay attention to and use digital currency apps. These applications provide users with a convenient way to manage and trade digital assets. So, what kind of software is a digital currency app? Let us have an in-depth understanding and take stock of the top ten digital currency apps in the world.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Quantitative Exchange Ranking 2025 Top 10 Recommendations for Digital Currency Quantitative Trading APPs Quantitative Exchange Ranking 2025 Top 10 Recommendations for Digital Currency Quantitative Trading APPs Apr 30, 2025 pm 07:24 PM

The built-in quantization tools on the exchange include: 1. Binance: Provides Binance Futures quantitative module, low handling fees, and supports AI-assisted transactions. 2. OKX (Ouyi): Supports multi-account management and intelligent order routing, and provides institutional-level risk control. The independent quantitative strategy platforms include: 3. 3Commas: drag-and-drop strategy generator, suitable for multi-platform hedging arbitrage. 4. Quadency: Professional-level algorithm strategy library, supporting customized risk thresholds. 5. Pionex: Built-in 16 preset strategy, low transaction fee. Vertical domain tools include: 6. Cryptohopper: cloud-based quantitative platform, supporting 150 technical indicators. 7. Bitsgap:

Is the digital currency app formal? Top 10 formal and legal virtual currency trading apps in the world Is the digital currency app formal? Top 10 formal and legal virtual currency trading apps in the world Apr 30, 2025 pm 07:09 PM

Recommended cryptocurrency trading platforms include: 1. Binance: the world's largest trading volume, supports 1,400 currencies, FCA and MAS certification. 2. OKX: Strong technical strength, supports 400 currencies, approved by the Hong Kong Securities Regulatory Commission. 3. Coinbase: The largest compliance platform in the United States, suitable for beginners, SEC and FinCEN supervision. 4. Kraken: a veteran European brand, ISO 27001 certified, holds a US MSB and UK FCA license. 5. Gate.io: The most complete currency (800), low transaction fees, and obtained a license from multiple countries. 6. Huobi Global: an old platform that provides a variety of services, and holds Japanese FSA and Hong Kong TCSP licenses. 7. KuCoin

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

How to download the Hong Kong Digital Currency Exchange app? The top ten digital currency exchange apps are included How to download the Hong Kong Digital Currency Exchange app? The top ten digital currency exchange apps are included Apr 30, 2025 pm 07:12 PM

The methods to download the Hong Kong Digital Currency Exchange APP include: 1. Select a compliant platform, such as OSL, HashKey or Binance HK, etc.; 2. Download through official channels, iOS users download on the App Store, Android users download through Google Play or official website; 3. Register and verify their identity, use Hong Kong mobile phone number or email address to upload identity and address certificates; 4. Set security measures, enable two-factor authentication and regularly check account activities.

How reliable is Binance Plaza? How reliable is Binance Plaza? May 07, 2025 pm 07:18 PM

Binance Square is a social media platform provided by Binance Exchange, aiming to provide users with a space to communicate and share information related to cryptocurrencies. This article will explore the functions, reliability and user experience of Binance Plaza in detail to help you better understand this platform.

phpMyAdmin's Function: Interacting with MySQL (SQL) phpMyAdmin's Function: Interacting with MySQL (SQL) May 07, 2025 am 12:16 AM

phpMyAdmin simplifies MySQL database management through the web interface. 1) Create databases and tables: Use graphical interface to operate easily. 2) Execute complex queries: such as JOIN query, implemented through SQL editor. 3) Optimization and best practices: including SQL query optimization, index management and data backup.

See all articles