php verification code

1. First save the verification code class as a file named ValidateCode.class.PHP  ;

<?php
// verification code class
class ValidateCode {
    private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//Random factor
    private $code;//Verification code
    private $codelen = 4;//Verification code length
    private $width = 130;//width
    private $height = 50;//height
    private $img;//Graphic resource handle
    private $font;//The specified font
    private $fontsize = 20;//Specify font size
    private $fontcolor;//Specify font color
    // initialize the constructor
    public function __construct() {
        $this->font = dirname(__FILE__).'/font/elephant.ttf';//Note that the font path must be written correctly, otherwise the picture will not be displayed
    }
    //generate random code
    private function createCode() {
        $_len = strlen($this->charset)-1;
        for ($i=0;$i<$this->codelen;$i++) {
            $this->code .= $this->charset[mt_rand(0,$_len)];
        }
    }
    //generate background
    private function createBg() {
        $this->img = imagecreatetruecolor($this->width, $this->height);
        $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
        imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
    }
    //generate text
    private function createFont() {
        $_x = $this->width / $this->codelen;
        for ($i=0;$i<$this->codelen;$i++) {
            $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
            imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
        }
    }
    //Generate lines and snowflakes
    private function createLine() {
        //line
        for ($i=0;$i<6;$i++) {
            $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
            imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
        }
        //Snowflake
        for ($i=0;$i<100;$i++) {
            $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
            imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
        }
    }
    // output
    private function outPut() {
        header('Content-type:image/png');
        imagepng($this->img);
        imagedestroy($this->img);
    }
    // Generate externally
    public function doimg() {
        $this->createBg();
        $this->createCode();
        $this->createLine();
        $this->createFont();
        $this->outPut();
    }
    //get verification code
    public function getCode() {
        return strtolower($this->code);
    }
}

 

2. Create a new file named captcha.php to call this class;

<?php
session_start();
require './ValidateCode.class.php'; //Include the class first, and the actual path is modified according to the actual situation.
$_vc = new ValidateCode(); //Instantiate an object
$_vc->doimg();
$_SESSION['authnum_session'] = $_vc->getCode();//The verification code is saved in SESSION

 3. html page

<?php
session_start();
//First of all, we need to open the session on the page,
//error_reporting(2047);
session_destroy();
//Remove the session so that a new session value can be taken every time;
//The effect of using seesion is good and it is very convenient
?>
<html>
<head>
<title>session image verification example</title>
<style type="text/css">
#login p{
margin-top: 15px;
line-height: 20px;
font-size: 14px;
font-weight: bold;
}
#login img{
cursor:pointer;
}
form{
margin-left:20px;
}
</style>
</head>
<body>
<form id="login" action="" method="post">
<p>This example is a session verification example</p>
<p>
<span>Verification code:</span>
<input type="text" name="validate" value="" size=10>
<img  title="点击刷新" src="./captcha.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>
</p>
<p>
<input type="submit">
</p>
</form>
<?php
//print the previous session;
//echo "上一个session:<b>".$_SESSION["authnum_session"]."</b><br>";
$validate="";
if(isset($_POST["validate"])){
$validate=$_POST["validate"];
echo "What you just entered is: ".$_POST["validate"]."<br>Status:";
if($validate!=$_SESSION["authnum_session"]){
/ / Determine whether the session value is consistent with the verification code entered by the user;
echo "<font color=red>Incorrect input</font>";
}else{
echo "<font color=green>Verified</font>";
}
}
?>

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327017727&siteId=291194637