1. First come to an html page index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Sign Up</title> </head> <body> <form action="./register.php" method="post"> <img src="Verify.php" onclick="this.src='Verify.php?'+new Date().getTime();" width="100" height="100"><br/> <input type= " text " name= " verify " placeholder= " Please enter the verification code in the picture " ><br/> <input type="submit" value="验证"> </form> </body> </html>
2. Verification code register.php
<?php /** * Alphanumeric verification code generation */ // Open session session_start(); // 1. Create a black canvas $image = imagecreatetruecolor( 100 , 30 ); // 2. Define the (background) color for the canvas $bgcolor = imagecolorallocate($image, 255 , 255 , 255 ); // 3. Fill color imagefill($image, 0 , 0 , $bgcolor); // 4. Set the verification code content // 4.1 Define the content of the verification code $content = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 " ; // 4.1 Create a variable to store the generated verification code data, which is convenient for users to submit and check $captcha = "" ; for ($i = 0 ; $i < 4 ; $i++ ) { // font size $fontsize = 10 ; // Font color $fontcolor = imagecolorallocate($image, mt_rand( 0 , 120 ), mt_rand( 0 , 120 ), mt_rand( 0 , 120 )); // Set font content $fontcontent = substr($content, mt_rand( 0 , strlen ($content)), 1 ); $captcha . = $fontcontent; // displayed coordinates $x = ($i * 100 / 4 ) + mt_rand( 5 , 10 ); $y = mt_rand( 5 , 10 ); // fill content to the canvas imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } $_SESSION["verifyimg"] = $captcha; // 4.3 Set the background noise element for ($$i = 0 ; $i < 200 ; $i++ ) { $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200)); imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor); } // 4.4 Set the interference line for ($i = 0 ; $i < 3 ; $i++ ) { $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200)); imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor); } // 5. Output image header information to the browser header( ' content-type:image/png ' ); // 6. Output the image to the browser imagepng($image); // 7. Destroy the image imagedestroy($image); ?>
3. Submit the verification verification code page register.php
<?php /** * Accept the verification code submitted by the user when logging in */ session_start(); // 1. Get the verification code submitted by the user $verify = $_POST[ " verify " ]; // 2. Check the verification code in the session with the verification code submitted by the user, when successful, the verification code is correct, and Destroy the previous session value, if unsuccessful, resubmit if (strtolower($_SESSION[ " verifyimg " ]) == strtolower($verify)){ echo "ok"; $_SESSION["verify"] = ""; }else{ echo "error"; } ?>
4. If you don't need interference lines or background interference elements, just annotate the content inside, and the effect is like this