This article describes how to use php to implement the website verification code function. It has good reference value. let's take a look at it below. This article mainly introduces how to use php to implement the website verification code function. It has good reference value. let's take a look at it together with the small editor.
Verification code is a common security measure for websites and a skill that is difficult for new webmasters. here I will introduce you to a simple and effective verification code implementation method.
Before getting started
Before the official start, we need to open the support for the php gd2 graphics library (in php. ini, search "php_gd2.dll", find "; extension = php_gd2.dll", and remove the semicolon at the beginning of the sentence ).
You can refer to: how to open the php gd2 Library
Core: img. php
This page generates a verification code and writes the correct value to the Session.
Random 4-digit verification code
$check=rand(1000,9999);
Write the generated verification code to the session
Session_start(); $_SESSION["check"] = $check;
Create an image
$im = imagecreate(80,30);
Because the background of this image is Black by default, we need to fill it with white.
imagefill($im,0,0,ImageColorAllocate($im, 255,255,255));
Use imageline to randomly draw two solid lines
$y1=rand(0,30); $y2=rand(0,30); $y3=rand(0,30); $y4=rand(0,30); imageline($im,0,$y1,70, $y3,000); imageline($im,0,$y2,70, $y4,000);
Draw text at random locations
$strx=rand(3,15); $stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,0,1),ImageColorAllocate($img,34,87,100)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,1,1),ImageColorAllocate($img,781,117,78)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,2,1),ImageColorAllocate($img,160,40,40)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,3,1),ImageColorAllocate($img,25,55,10));
Output Image
Header("Content-type: image/PNG"); ImagePNG($img);
Complete Code
User interface: index. php
I think everyone knows how to do this. I will give the code directly.
The above code passes the value entered by the user to "action. php ".
Check: action. php
In this step, we need to compare the user input value with the value in the session.
Equal, the output is "correct"
Not equal. the output is "incorrect"
The above is a detailed explanation of the code for implementing the website verification code using php. For more information, see PHP Chinese website (www.php1.cn )!