PHP implementation verification code (with source code)

The source code is directly attached here,

For details, please refer to the article http://www.cnblogs.com/jianxian/p/8596340.html

 

 
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <form action="./login.php" method="post">
        <img src="image_captcha.php"  onclick="this.src='image_captcha.php?'+
            new Date().getTime();" width="200" height="200"><br/>
        <input type="text" name="captcha" placeholder="Please enter the captcha in the picture"><br/>
        <input type="submit" value="验证">
    </form>
</body>
</html>

   Sibling file login.php

<?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. Check the verification code in the session with the verification code submitted by the user. When successful, it will prompt that the verification code is correct.
//And destroy the previous session value, if unsuccessful, resubmit
// echo strtolower($captcha);
// echo strtolower($_SESSION);
if(strtolower($_SESSION["captcha"]) == strtolower($captcha)){
    echo "Verification code is correct!";
    $_SESSION["captcha"] = "";
}else{
    echo "Incorrect verification code submission!";
}
?>

   Sibling file image_captcha.php

<?php
/**
 * Alphanumeric verification code generation
 */
// open session
session_start();
//1. Create a black canvas
$image = imagecreatetruecolor(100, 30);
 
//2. Define (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 the canvas with content
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
$_SESSION["captcha"] = $captcha;
 
//4.3 Set background interference elements
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);
}
 ob_clean();//The original program does not have this column
//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 picture
imagedestroy($image);  
?>

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326095818&siteId=291194637