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.输出图片

  • 相关阅读:
    python-super方法
    python--mixin
    python中将输出写入文件
    一致性hash算法
    mysql之触发器trigger
    python内置函数
    python3.5+Django2.2+pymysql+mysql
    File "D:PythonPython37-32libsite-packagesdjangoviewsdebug.py", line 332, in get_traceback_html   t = DEBUG_ENGINE.from_string(fh.read())
    RuntimeError: You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to po
    python:面向对象
  • 原文地址:https://www.cnblogs.com/zox2011/p/2176251.html
Copyright © 2011-2022 走看看