|
原程序:基于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>
复制代码 |
评分
-
查看全部评分
|