<?php
/*****************************************************************
 * zh-code.php
 * @author  visam
 * @email   zd_168 (at) 126.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;$i<strlen($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($c<0x80)
        $str.=$c;
    elseif($c<0x800)
    {
        $str.=(0xC0|$c>>6);
        $str.=(0x80|$c&0x3F);
    }
    elseif($c<0x10000)
    {
        $str.=(0xE0|$c>>12);
        $str.=(0x80|$c>>6&0x3F);
        $str.=(0x80|$c&0x3F);
    }
    elseif($c<0x200000)
    {
        $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语法:<img src="excode.php">调用就可以了,在服务端做验证时取session存储的验证字符与用户提交的字符进行比较,相同则通过验证。