首页 > php代码 > php生成中文验证码程序

php生成中文验证码程序

<?php
session_start();
// 取得随机字符
function getRandStr($length = 4, $mode = 1) {
    $str1 = '123456789';
    $str2 = 'abcdefghijklmnopqrstuvwxyz';
    $str3 = 'ABCDEFGHIJKLMNPQRSTUVWXYZ';
    $str4 = '_';
    $str5 = '`~!@#$%^&*()-+=\|{}[];:\'",./?';
    $str6 = '孙继海立功了不要给日本人任何机会伟大的中国右后
 卫他继承中华民族的光荣传统董存瑞黄继光秋少云在这一刻灵魂
 突起孙继海一个人他代表了中华民族足球悠久的传统在这一刻
 他不是一个人的战斗他不是一个人李毅大帝面对这个点球他面对的
 是全世界中国球迷的目光和期待川口能活曾经在附加赛当中世界杯
 预选塞的附加赛当中扑出过无数个点球大帝肯定深知这一点他还
 能够微笑着面对他面前的这个人吗秒钟以后他会是怎么的表情球
 进了比赛结束了中国队队获得了胜利日本队他们没有再一次的到
 在济科的面前伟大的中华人民共和国伟大的中国队的右后卫小猫
 今天生日快乐中华人民共和国万岁日本队也许会后悔的济科他
 在下半场多打一人的情况下打得太保守太沉稳了他失去了自己
 的勇气面对中国足球悠久的历史和传统他没有在拿出小组赛那样
 冲撞门将的作风终于自惭形秽他们该回家了他们不用回遥远的
 日本因为他们会选择集体剖腹自杀再见';
switch ($mode) {
case '0':
$str = $str1 . $str2 . $str3 . $str4;
break;
case '1':
$str = $str1;
break;
case '2':
$str = $str2;
break;
case '3':
$str = $str3;
break;
case '4':
$str = $str2 . $str3;
break;
case '5':
$str = $str1 . $str2;
break;
case '6':
$str = $str1 . $str3;
break;
case '7':
$str = $str1 . $str2 . $str3;
break;
case '8':
$str = $str1 . $str2 . $str3 . $str4 . $str5;
break;
case '9':
$str = $str6;
break;
default:
$str = $str1 . $str2 . $str3 . $str4;
break;
    }
    $result = "";
    if ($mode == 9) {
        $tmp1 = $length / 2;
        $tmp = explode(".", strlen($str) / 2);
        for ($i = 0; $i < $tmp1; $i++) {
            $result.= Substr($str, mt_rand(0, $tmp[0]) * 2 - $length, 2);
        }
    } else {
        for ($i = 0; $i < $length; $i++) {
            $result.= $str[mt_rand(0, strlen($str) - 1) ];
        }
    }
    return $result;
}
//#
// 生成验证码图片
//
function createAuthNumImg($randStr, $isGB = false) {
    Header("Content-type: image/PNG");
    $im = imagecreate(50, 20);
    $black = ImageColorAllocate($im, 0, 0, 0);
    $white = ImageColorAllocate($im, 255, 255, 255);
    $gray = ImageColorAllocate($im, 0, 100, 150);
    imagefill($im, 50, 19, $gray);
    for ($i = 0; $i < 80; $i++) // 加入干扰象素
    {
        $randcolor = ImageColorallocate($im, rand(0, 255) , rand(0, 255) , rand(0, 255));
        imagesetpixel($im, rand() % 50, rand() % 19, $randcolor);
    }
    if ($isGB != false) // 采用中文验证码
    {
        require_once ("GB2312toUTF8.inc.php");
        $GB2312toUTF = new GB2312toUTF8();
        imagettftext($im, 10, 5, 5, 16, $white, "simsun.ttc", $GB2312toUTF->gb2utf8($randStr));
    } else imagestring($im, 8, 8, 3, $randStr, $white); // 将验证码绘入图片
    ImagePNG($im);
    ImageDestroy($im);
    $_SESSION['chknum'] = $randStr; // 保存验证码
    
}
$m = $_GET["m"];
if (empty($m)) {
    $m = 9;
}
createAuthNumImg(getRandStr("6", $m) , 1)


本文链接:http://www.phprm.com/code/30d0d638a85364dc4941db20d0260247.html

收藏随意^^请保留教程地址.

标签:none

发表留言