zoukankan      html  css  js  c++  java
  • php实现验证码

    简单纯数字验证码

    <?php
    $image = imagecreate(50,25);
    imagecolorallocate($image,0,0,0);
    $color =  imagecolorallocate($image,255,255,255);
    $code = mt_rand(1000,9999);
    session_start();
    $_SESSION['code'] = $code;
    imagestring($image,4,5,5,$code,$color);
    header("content-type:image/png");
    imagepng($image);
    ?>
    


    数字+字母验证码:

    <?php
    $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
    $image=imagecreate(50,25);
    imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
    $color = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
    for($i=1;$i<=4;$i++) {
        $date=$str[mt_rand(0,strlen($str)-1)];
        $code.=$date;
    }
    session_start();
    $_SESSION['code'] = $code;
    imagestring($image,4,8,4,$code,$color);
    for($i=1;$i<=30;$i++) {
    imagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
    }
    for($i=1;$i<=mt_rand(1,5);$i++) {
    imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150));
    }
    header("content-type:image/png");
    imagepng($image);
    ?>
    

    数字+字母验证码(各字母颜色不同):

    <?php
    $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
    $image=imagecreate(50,25);
    imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
    $color[0] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
    $color[1] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
    $color[2] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
    $color[3] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
    for($i=0;$i<4;$i++) {
        $date=$str[mt_rand(0,strlen($str)-1)];
        $code.=$date;
        imagestring($image,5,6+$i*10,4,$code[$i],$color[$i]);
    }
    session_start();
    $_SESSION['code'] = $code;
    for($i=1;$i<=30;$i++) {
    imagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
    }
    for($i=1;$i<=mt_rand(1,5);$i++) {
    imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150));
    }
    header("content-type:image/png");
    imagepng($image);
    ?>
    

    流程:

    1.设置字符集

    2.创建图片资源集

    3.设置图片背景颜色

    4.随机取出字符集中的字符

    5.开启SESSION用SESSION变量记录取出的字符集

    6.把取出的字符集加入图片,增加字体颜色

    7.添加干扰点

    8.添加干扰线

    9.填写header文件头

    10.输出图片

  • 相关阅读:
    How to install VXDIAG Honda, Toyota and JLR SDD software
    16% off MPPS V16 ECU tuning tool for EDC15 EDC16 EDC17
    Cummins INSITE locked and ask for verification code
    How to use BMW Multi Tool 7.3 to replace lost key for BMW X1
    Bleed Brake Master Cylinder with Intelligent Tester IT2
    Porsche Piwis Tester II “No VCI has been detected”,how to do?
    Creader VIII VS. Creader VII+
    How to solve GM MDI cannot complete the installation
    汽车OBD2诊断程序开发 (原文转载,思路很清晰!)
    汽车节温器单片机开发思路
  • 原文地址:https://www.cnblogs.com/zox2011/p/2176251.html
Copyright © 2011-2022 走看看