文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>PHP中文验证码

PHP中文验证码

时间:2007-06-11  来源:linxh

原文转自:
http://hi.baidu.com/aspboy/blog/item/34107ecf2dad363df8dc61fc.html

     几个GD函数堆砌出来的玩意,只要搞定怎样把中文写到图片上就可以了,因为GD函数只接受UTF8格式编码的文字,所以在写文字前首先要进行编码转换。PHP自带的iconv和mbstring库都可以完成这项工作,但一般的虚拟主机很少支持,所以要自己写个,像这里用gb2utf8完成这项工作。当然如果你的页面就是以UTF8格式存储的,就可以省去这些额外工作。


?php
/*****************************************************************
* excode.php
* @author 风舞
* @email wjiujun (at) gmail.com
* @note 一个中文附加码
*****************************************************************/
$RANDCODE=array('宠','辱','不','惊','静','看','庭','前','花','开','花','落','得','失','无','意','漫','望','天','外','云','卷','云','舒');
$CODETABLE=array();
$fp=fopen("gb2312.txt","r");
while($line=fgets($fp))
    $CODETABLE[hexdec(substr($line,0,6))]=substr($line,7,6);
fclose($fp);
//gb2312转utf8
function gb2utf8($gbstr)
{
    global $CODETABLE;
    if(trim($gbstr)=="")
        return $gbstr;
    $ret="";
    $utf8="";
    while($gbstr)
    {
        if(ord(substr($gbstr,0,1))>127)
        {
            $thisW=substr($gbstr,0,2);
            $gbstr=substr($gbstr,2,strlen($gbstr));
            $utf8="";
            @$utf8=u2utf8(hexdec($CODETABLE[hexdec(bin2hex($thisW))-0x8080]));
            if($utf8!="")
            for($i=0;$istrlen($utf8);$i+=3)
                $ret.=chr(substr($utf8,$i,3));
        }
        else
        {
            $ret.=substr($gbstr,0,1);
            $gbstr=substr($gbstr,1,strlen($gbstr));
        }
    }
    return $ret;
}
//unicode转utf8
function u2utf8($c)
{
    $str="";
    if($c0x80)
        $str.=$c;
    elseif($c0x800)
    {
        $str.=(0xC0|$c>>6);
        $str.=(0x80|$c&0x3F);
    }
    elseif($c0x10000)
    {
        $str.=(0xE0|$c>>12);
        $str.=(0x80|$c>>6&0x3F);
        $str.=(0x80|$c&0x3F);
    }
    elseif($c0x200000)
    {
        $str.=(0xF0|$c>>18);
        $str.=(0x80|$c>>12&0x3F);
        $str.=(0x80|$c>>6&0x3F);
        $str.=(0x80|$c&0x3F);
    }
    return $str;
}
//生成附加码
function create_excode($length)
{
global $RANDCODE;
    header("content-type: image/png");
$image_x=$length*30; //图片宽度
$image_y=40; //图片高度
    $noise_num=80*$length; //杂点数量
    $line_num=$length-2; //干扰线数量
$image=imagecreate($image_x,$image_y);
imagecolorallocate($image,0xff,0xff,0xff); //设定背景颜色
$rectangle_color=imagecolorallocate($image,0xAA,0xAA,0xAA); //边框颜色
$noise_color=imagecolorallocate($image,0x00,0x00,0x00); //杂点颜色
$font_color=imagecolorallocate($image,0x00,0x00,0x00); //字体颜色
$line_color=imagecolorallocate($image,0x33,0x33,0x33); //干扰线颜色
//加入杂点
    for($i=0;$i$noise_num;$i++)
  imagesetpixel($image,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
$font_face="simkai.ttf"; //字体
    $x=2;
    $session_code='';
    for($i=0;$i$length;$i++)
    {
        $code=$RANDCODE[mt_rand(0,count($RANDCODE)-1)];
     imagettftext($image,18,mt_rand(-6,6),$x,29,$font_color,$font_face,gb2utf8($code));
        $x+=30;
        $session_code.=$code;
    }
    @session_start();
    $_SESSION['excode']=$session_code; //把附加码的值放在session中
    //加入干扰线
    for($i=0;$i$line_num;$i++)
     imageline($image,mt_rand(0,$image_x),mt_rand(0,$image_y),
                    mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
imagerectangle($image,0,0,$image_x-1,$image_y-1,$rectangle_color); //加个边框
imagepng($image);
imagedestroy($image);
}
create_excode(6);
?>
    使用的时候直接用html语法:调用就可以了,在服务端做验证时取session存储的验证字符与用户提交的字符进行比较,相同则通过验证。
    注:程序用到的simkai.ttf是windows中常见的楷体,当然你也可以换成别的字体,但一定要支持中文的;
gb2312.txt是gb2312和utf8的编码对照表,

文件:
gb2312.rar
大小:
33KB
下载:
下载




相关阅读 更多 +
排行榜 更多 +
jojo的奇妙冒险手机版下载

jojo的奇妙冒险手机版下载

飞行射击 下载
雪糕工厂 v9.87.13.02 安卓版

雪糕工厂 v9.87.13.02 安卓版

休闲益智 下载
雪糕工厂 v9.87.13.02 安卓版

雪糕工厂 v9.87.13.02 安卓版

休闲益智 下载