• php实现图片加法验证码


    
    /**
     * Created by PhpStorm.
     * User: finejade
     * Date: 2023-10-19
     * Time: 11:07
     */
    session_start(); // 启动Session
    require_once('common.php');
    session_start();
    /*定义头文件为图片*/
    header("Content-type: image/png");
    /*生成验证码*/
    /*创建图片设置字体颜色*/
    $w = 70;
    $h = 25;
    $im = imagecreate($w, $h);
    $red = imagecolorallocate($im, 255, 255, 255);
    $white = imagecolorallocate($im, 255, 255, 255);
    /*随机生成两个数字*/
    $num1 = rand(1, 20);
    $num2 = rand(1, 20);
    $_SESSION ["captcha"] = $num1 + $num2;
    /*设置图片背景颜色*/
    $gray = imagecolorallocate($im, 118, 151, 199);
    $black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
    /*创建图片背景*/
    imagefilledrectangle($im, 0, 0, 100, 24, $black);
    /*在画布上随机生成大量点*/
    for ($i = 0; $i < 80; $i++) {
        imagesetpixel($im, rand(0, $w), rand(0, $h), $gray);
    }
    /*将计算验证码写入到图片中*/
    imagestring($im, 5, 5, 4, $num1, $red);
    imagestring($im, 5, 30, 3, "+", $red);
    imagestring($im, 5, 45, 4, $num2, $red);
    imagestring($im, 5, 70, 3, "=", $red);
    imagestring($im, 5, 80, 2, "?", $white);
    /*输出图片*/
    imagepng($im);
    imagedestroy($im);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    代码中校验

    	session_start();
        $captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
         if ($_SESSION ["captcha"] != $captcha) {
                return error('图形验证码不正确');
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    前端代码

    <img src="captcha.php" width="70" height="30"  id="valid-img" title="点击再换一张" class="vail_img" alt="验证码图片"/>
    
    • 1
        $("#valid-img").click(function () {
            $(this)[0].src ='captcha.php?'+Math.random();
        })
    
    • 1
    • 2
    • 3
  • 相关阅读:
    VueCLI脚手架
    Python时间转换:X秒 --> 时:分:秒
    从零入门机器学习之Linux系统详解
    交换机和路由器技术-21-RIP路由协议
    DuDuTalk:4G语音工牌,如何实现家庭上门维修服务过程的智能化管理?
    C#datagridview专题
    Bytebase加入阿里云PolarDB开源数据库社区
    招投标系统软件源码,招投标全流程在线化管理
    范数-空间范数
    【c++】list详细讲解
  • 原文地址:https://blog.csdn.net/xxpxxpoo8/article/details/133931841