用户
 找回密码
 入住 CI 中国社区
搜索
查看: 8469|回复: 10
收起左侧

[插件 Plugin] 验证码插件

[复制链接]
发表于 2009-8-18 11:00:30 | 显示全部楼层 |阅读模式
原程序:基于PHPOK
Plugin文件代码:
PHP复制代码
 
<?php
#===========================================================
#       Filename: system/plugins/vcode_pi.php
#       Note    : 备注
#       Version : 3.0
#       Author  : qinggan
#       Update  : 2009-08-17
#===========================================================
if(!function_exists("Verification_code"))
{
        function Verification_code($session)
        {
                $x_size=90;
                $y_size=30;
                if(function_exists("imagecreate"))
                {
                        $aimg = imagecreate($x_size,$y_size);
                        $back = imagecolorallocate($aimg, 255, 255, 255);
                        $border = imagecolorallocate($aimg, 0, 0, 0);
                        imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
                        $txt="0123456789";
                        $txtlen=strlen($txt);
 
                        $thetxt="";
                        for($i=0;$i<4;$i++)
                        {
                                $randnum=mt_rand(0,$txtlen-1);
                                $randang=mt_rand(-20,20);       //文字旋转角度
                                $rndtxt=substr($txt,$randnum,1);
                                $thetxt.=$rndtxt;
                                $rndx=mt_rand(2,7);
                                $rndy=mt_rand(0,9);
                                $colornum1=($rndx*$rndx*$randnum)%255;
                                $colornum2=($rndy*$rndy*$randnum)%255;
                                $colornum3=($rndx*$rndy*$randnum)%255;
                                $newcolor=imagecolorallocate($aimg, $colornum1, $colornum2, $colornum3);
                                imageString($aimg,5,$rndx+$i*21,5+$rndy,$rndtxt,$newcolor);
                        }
                        unset($txt);
                        $thetxt = strtolower($thetxt);
                        $session->set_userdata("login_vcode",md5($thetxt));
                        imagerectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $border);
 
                        $newcolor="";
                        $newx="";
                        $newy="";
                        $pxsum=100;     //干扰像素个数
                        for($i=0;$i<$pxsum;$i++)
                        {
                                $newcolor=imagecolorallocate($aimg, mt_rand(0,254), mt_rand(0,254), mt_rand(0,254));
                                imagesetpixel($aimg,mt_rand(0,$x_size-1),mt_rand(0,$y_size-1),$newcolor);
                        }
                        header("Pragma:no-cache");
                        header("Cache-control:no-cache");
                        header("Content-type: image/png");
                        imagepng($aimg);
                        imagedestroy($aimg);
                        exit;
                }
        }
}
?>
复制代码

Controller代码
PHP复制代码
 
<?php
#===========================================================
#       Filename: system/application/controllers/vcode.php
#       Note    : 验证码
#       Version : 3.0
#       Author  : qinggan
#       Update  : 2009-08-17
#===========================================================
class Vcode extends Controller
{
        function Vcode()
        {
                parent::Controller();
        }
 
        function index()
        {
                $this->load->plugin("vcode");
                echo Verification_code($this->session);
        }
}
?>
复制代码

模板代码(我这里使用的是Smarty,有所出入请自行修改噢)
HTML复制代码
 
<script type="text/javascript">
function reload_vcode(v)
{
        var url = "index.php/vcode/index/";
        var rand = Math.random();
        url += rand;
        v.src = url;
}
</script>
<div><img src="index.php/vcode" border="0" onclick="reload_vcode(this)" style="cursor:pointer;"></div>
 
复制代码

评分

参与人数 1威望 +5 收起 理由
Hex + 5 原创内容

查看全部评分

发表于 2009-8-19 17:39:05 | 显示全部楼层
唉,ctl+v习惯了. 沙发
发表于 2009-8-25 15:29:28 | 显示全部楼层
楼主是不是发布过php cms的那位情感?
发表于 2009-8-25 15:30:45 | 显示全部楼层
情感CMS 我也用过,很轻量,很快速。
发表于 2009-8-25 17:17:03 | 显示全部楼层
本帖最后由 乌有 于 2009-8-25 17:22 编辑

搞明白了..
还要MD5对比..
 楼主| 发表于 2009-9-1 11:51:54 | 显示全部楼层
晕菜~~PHPOK这东东仅适合用来做企业站的~~呵呵
发表于 2009-9-1 12:46:07 | 显示全部楼层
适合的永远是最好的。
发表于 2010-3-24 17:06:02 | 显示全部楼层
这个插件很不错,不过有问题,其中用imageString函数生成的字符串的字体大小最大只能调到5,而我试用了别的函数:imagettftext,可是这个函数需要导入一个字体文件,并且还不能用http://开头的,只能使用相对路径,但在CI里又用不了相对路径,请问有什么好的方法吗?我的目的就是使得到的验证码图片里的文字字体大点,谢谢!
发表于 2010-12-30 15:37:35 | 显示全部楼层
这个东东不错的 很实用的
发表于 2012-11-15 21:11:31 | 显示全部楼层
帅啊

本版积分规则