Use php to generate number and letter combination verification code

Projects often encounter a series of security verification strategies such as login verification, payment verification and so on. There are various implementation methods. Let's explain how to use php to generate a simple text + number combination verification code: 

The language used is php, gd library

Principle explanation:

a> In essence, the verification code is randomly generated on the server side and stored in $_SESSION.

b> Then write the verification code on the picture, send the picture to the client, and the user enters the verification code on the picture and submits it to the server.

c> The server compares with the information stored in $_SESSION, if it is consistent, it passes, otherwise it does not pass.

 

 

Divided into two steps:

1) The first step is to generate a verification code on the server side

2) The client uses the verification code

Step 1: How to generate a verification code on the server side: <code.php>

<?php
session_start(); //1>session_start() must be placed at the top, 2>multiple servers, consider centralized management of session information
 //phpinfo();//Print out php information
 //1. Generate a base map (width: 100; height: 30)
$image=imagecreatetruecolor(100,30);
$bgcolor=imagecolorallocate($image,255,255,255);//Brush coloring
imagefill($image,0,0,$bgcolor);//Color the basemap
 
// randomly generate four numbers
/*
for($i=0;$i<4;$i++){
    $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
    $fontcontent=rand(0,9);//Content of verification code
    $fontsize=6;//font size
    $x=($i*100/4)+rand(5,10);//x axis
    $y=rand(5,10);//Y轴
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);//Use imagestring() to write text on the canvas
}
*/
$captch_code="";
for($i=0;$i<4;$i++){
    $fontsize=6;
    $fontcolor=imagecolorallocate($image,rand(20,100),rand(30,100),rand(10,200));
    $str="abcdefghkmnpwsert1234567890";//Give a string to generate a random verification code
    $fontcontent=substr($str,rand(0,strlen($str)),1);//Intercept one character at a time
    $captch_code.=$fontcontent;//Splicing
    $x=($i*100/4)+rand(5,10);
    $y=rand(5,10);
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode']=$captch_code;//Save in session
//add distraction point
for($i=0;$i<100;$i++){
    $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
    imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor);
}
//Add interference line
for($i=0;$i<3;$i++){
    $linecolor=imagecolorallocate($image,rand(80,200),rand(80,200),rand(80,200));
    imageline($image,rand(5,95),rand(5,25),rand(5,95),rand(5,25),$linecolor);
}ob_clean();//Clear the cache
header("content-type:".'image/png');//The fixed format of the header file of the output image (can be image/jpg, image/png,)
imagepng($image);//Output an image to the browser
imagedestroy($image);//Destroy the basemap in memory
 
//session_start

  Step 2: Return the generated verification code image to the client <form.php>

<?php
header("Content-type:text/html;charset=utf-8");//Avoid garbled characters
if(isset($_REQUEST['authcode'])){
    session_start();//session_start() must be used before using $_SESSION
    if(strtolower($_REQUEST['authcode'])==$_SESSION["authcode"]){//$_SESSION['authcode'] is the verification code stored on the server, $_REQUEST['authcode'] gets the client entered information
        echo "<font color='#000cc'>The input is correct</font>";
    }
    else{
        echo "<font color='#0000cc'>input error</font>";
    }
    exit();
}
?>

  

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Verification code is correct</title>
    </head>
</html>
<body>
    <form action="./form.php">
        <p>Captcha image:<br>
//Click the verification code image itself to switch the verification code (requirement 1)
    //Generate random numbers for easy access to the server, if the address is the same, the browser will not refresh the access
        <img id="captCode" src="./code.php?=<?php echo rand();?>" width="100px;height:100px;" alt=""
                 onclick="this.src='code.php?+ Math.random()'">  
//Click 'click to switch' to switch the verification code (requirement 2)
            <img id="captCode" src="./code.php?=--><?php //echo rand();?><!--" width="100px;height:100px;" alt="">         
            <a href="javascript:void(0)" onclick="document.getElementById('captCode').src='code.php?=<?php echo rand();?>'">点击切换?</a>
        </p>
        <p>Please enter the content of the verification code:
            <input type="text" name="authcode" value=""/>
        </p>
        <P>
            <input type="submit" value="提交" style="padding:6px 20px;"/>
        </P>
    </form>
</body>

  

A few exceptions:

1): In case of garbled characters: header("Content-type:text/html;charset=utf-8");//Avoid garbled characters

2): If the captcha image cannot be displayed on the front end: it may be a cache problem:

Facts screenshot:

 

Guess you like

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