Home > Backend Development > PHP Tutorial > Simple implementation method of verification code in thinkPHP

Simple implementation method of verification code in thinkPHP

不言
Release: 2023-03-30 14:48:02
Original
2545 people have browsed it

This article mainly introduces the simple implementation method of verification code in thinkPHP, and analyzes the implementation principles, steps and calling techniques of thinkPHP verification code in the form of examples. Friends in need can refer to the following examples

Learn the simple implementation method of verification code in thinkPHP. Share it with everyone for your reference, the details are as follows:

The running effect diagram is as follows:

1.php side generates verification code function

public function verify(){
    // 验证码
    import("@.Util.Image");
    Image::buildImageVerify(4,1,'png',40,20,'verify');
}
/**
* 生成图像验证码
* @static
* @access public
* @param string $length 位数
* @param string $mode 类型
* @param string $type 图像格式
* @param string $width 宽度
* @param string $height 高度
* @return string
*/
static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify') {
    import('ORG.Util.String');
    $randval = String::randString($length, $mode);
    session($verifyName, md5($randval));
    $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
    if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
      $im = imagecreatetruecolor($width, $height);
    } else {
      $im = imagecreate($width, $height);
    }
    $r = Array(225, 255, 255, 223);
    $g = Array(225, 236, 237, 255);
    $b = Array(225, 236, 166, 125);
    $key = mt_rand(0, 3);
    $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);  //背景色(随机)
    $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
    imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
    imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
    $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
    // 干扰
    for ($i = 0; $i < 10; $i++) {
      imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
    }
    for ($i = 0; $i < 25; $i++) {
      imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
    }
    for ($i = 0; $i < $length; $i++) {
      imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
    }
    Image::output($im, $type);
}
Copy after login

While generating the image, pass it into the session.

2. Page side

<img id="verifyImg" src="{sh::U(&#39;Agent/Login/verify&#39;)}" onClick="changeVerify()" title="点击刷新验证码" /></p>
Copy after login

Call directly from src.

Click to trigger changes.

function changeVerify(){
 verifyURL = "{sh::U(&#39;Agent/Login/verify&#39;)}";
 $("#verifyImg").attr("src",verifyURL);
 return false;
}
Copy after login

3. Background verification, compare the post field and the verification code in the session to see if they are consistent.

if($_SESSION[&#39;verify&#39;] != md5($_POST[&#39;verify&#39;])) {
  $this->error(&#39;验证码错误!&#39;);
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to implement phpqrcode to generate QR code with logo in Thinkphp3.2.3

thinkphp Comprehensive analysis of the built-in verification code

thinkPHP framework implements the method of generating barcodes

The above is the detailed content of Simple implementation method of verification code in thinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template