PHP implements verification code function

 

 

Login page code login:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登陆</title>
</head>
<body>
    <form action="./test.php" method="post">
        <img src="image_captcha.php"  onclick="this.src='image_captcha.php?'+new Date().getTime();" width="140" height="35"><br/>
        < INPUT type = "text" name = "captcha" placeholder = "Please enter the image verification code" > < br /> 
        < INPUT type = "Submit" value = "Verify" > 
    </ form > 
</ body > 
< / html >

 

Generate verification code:

<? php
 / * * 
 * Generation of verification codes for letters + numbers 
 * / 
// Open session 
session_start ();
 // 1. Create a black canvas 
$ image = imagecreatetruecolor (100, 30 ); 

// 2. Define the canvas (background ) Color 
$ bgcolor = imagecolorallocate ( $ image , 255, 255, 255 ); 

// 3. Fill color 
imagefill ( $ image , 0, 0, $ bgcolor ); 

// 4. Set verification code content 

// 
4.1 define verification code The content of $ content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ; 

// 4.1 Create a variable to store the verification code data generated, 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 into the canvas 
    imagestring ( $ image , $ fontsize , $ x , $ y , $ fontcontent , $ fontcolor ); 
} 

$ _SESSION ["image_captcha"] = $ captcha ; 

// 4.3 Set the background interference 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 设置干扰线
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. The browser outputs the image header information 
header ('content-type: image / png' ); 

// 6. Output the image to the browser 
imagepng ( $ image ); 

// 7. Destroy the image 
imagedestroy ( $ image );

Verify the input code:

<? php
 / * * 
 * Accept the verification code submitted by the user when logging in 
 * / 
session_start ();
 // 1. Get the verification code submitted by the user 
$ captcha = $ _POST ["captcha" ];
 // 2. Transfer the session verification code and verification code submitted by the user to check, prompted when successful authentication code is correct, then jump back to resubmit unsuccessful 
iF ( strtolower ( $ _SESSION [ "image_captcha"]) == strtolower ( $ captcha )) {
     echo " <h5> yes! The verification code is correct! </ h5> " ; 
} else {
     echo " NO! The verification code was submitted incorrectly! " ;
     header (" refresh: 3; url =. / login.php ");// Enter the error to jump to the login page
}

 

Guess you like

Origin www.cnblogs.com/niusdtz/p/12683415.html