Then, press Ctrl + F on the browser to find the gd option to verify whether you have installed the extension. if not, you need to install the extension on your own. Expansion
We need to enable gd expansion. you can use the following code to check whether gd expansion is enabled.
Then in the browserCtrl+FFind the gd option to verify whether you have installed the extension. if not, you need to install the extension on your own.
Background image imagecreatetruecolor
Black background is generated by default.
ImagecolorallocateCreate a fill color and attach it using imagefill (image, x, y, color.
Imagepng
Before using this method, you must first set the header information. Otherwise, the image will not be displayed normally.
Imagedestory (image)
Releasing resources in a timely manner reduces the pressure on server requests.
Simple digital verification code imagecolorallocateGenerate color information to facilitate future authorization.
$fontcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
ImagestringWrite the content information to the corresponding position of the image.
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
Increase recognition interference points// Generate some interference points. here there are 200 for ($ I = 0; $ I <200; $ I ++) {$ pointcolor = imagecolorallocate ($ image, rand (50,255), rand (50,255), rand (50,255); imagesetpixel ($ image, rand (0,100), rand (), $ pointcolor );}Add line// Generate some interference lines 5 for ($ I = 0; $ I <5; $ I ++) {// Set it to a light color line, prevents leeching. $ linecolor = imagecolorallocate ($ image, rand (50,255), rand (50,255), rand (50,255); imageline ($ image, rand ), rand (), $ linecolor );}Mixed verification code When the verification code is used to enable the session
Note: You must start the session.
Verification principleThe verification process is to compare the verification code entered by the client with the verification code in the session domain. That is:
if(isset($_REQUEST['checkcode'])){ session_start(); if($_REQUEST['checkcode']==$_SESSION['checkcode']){ echo "Success!"; }else{ echo "Failed!"; } exit(); }Optimization verificationHowever, it is easy to verify the case sensitivity of letters. So we need to convert all the user input values into lowercase letters.
if(strtolower($_REQUEST['checkcode'])==$_SESSION['checkcode']){···}Verification code generated in small cases Form verification
Verification code informationScript function change () {document. getElementById ("image_checkcode"). src = './store. php? R = '+ Math. random ();} script SummaryFinally, let's make a summary.
Using php to create verification codes requires the support of gd extension.
Use imagecreatetruecolor to generate the background color and use imagefill to fill in a color generated by imagecolorallocate.
Use imagestring to combine the verification code with the background image
Use imagesetpixel to add interference points
Use imageline to add interference lines
Enable the session_start () method at the beginning before using session.
Use JavaScript to dynamically modify the src of the verification code to meet the user's "change one" requirement.
The above is a detailed description of the principles of PHP verification code implementation. For more information, see other related articles in the first PHP community!