简单易用的验证码类库
本帖最后由 wintion 于 2010-3-26 17:32 编辑1)与CI紧密结合,使用CI的session
2)可以更改字体
3)可以选择验证码长度、内容
4)颜色自定义
5)干扰元素自定义
样式1:数字字母验证码
http://www.kandejian.com/wp-content/uploads/2010/03/1.jpg
样式2:纯字母验证码
http://www.kandejian.com/wp-content/uploads/2010/03/2.jpg
样式3:纯数字验证码
http://www.kandejian.com/wp-content/uploads/2010/03/3.jpg
以下所示文件打包下载:
1、创建类库 Authcode.php
<?php
// ------------------------------------------------------------------------
/**
* CodeIgniter Authcode Class
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author wintion@gmail.com
* @link http://www.kandejian.com
*/
class Authcode
{
var $CI;
var $fontPath;//字体路径
var $image;
var $charLen = 4; //生成几位验证码
var $arrChr = array();//验证码字符
var $width = 83; //图片宽
var $height = 24; //图片高
var $bgcolor = "#ffffff"; //背景色
var $showNoisePix = true; //生成杂点
var $noiseNumPix = 80; //生成杂点数量
var $showNoiseLine = true; //生成杂线
var $noiseNumLine = 2; //生成杂线数量
var $showBorder = true; //边框,当杂点、线一起作用的时候,边框容易受干扰
var $borderColor = "#000000";
function Authcode()
{
$this->CI = & get_instance();
$this->fontPath = realpath(dirname(__FILE__) . '/fonts/'); //字体文件
//$this->arrChr = array_merge(range(1, 9) , range('A', 'Z'));//数字字母验证码
//$this->arrChr = range('A', 'Z');//纯字母验证码
$this->arrChr = range(0, 9);//纯数字验证码
}
/**
* 显示验证码
*
*/
function show()
{
$this->image = imageCreate($this->width, $this->height);
$this->back = $this->getColor($this->bgcolor);
imageFilledRectangle($this->image, 0, 0, $this->width, $this->height, $this->back);
$size = $this->width / $this->charLen - 4;
if ($size > $this->height) {
$size = $this->height;
}
$left = ($this->width - $this->charLen * ($size + $size / 10)) / $size + 5;
for($i = 0; $i < $this->charLen; $i ++) {
$randKey = rand(0, count($this->arrChr) - 1);
$randText = $this->arrChr[$randKey];
$code .= $randText;
$textColor = imageColorAllocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
$font = $this->fontPath . '/' . rand(1, 5) . ".ttf";
$randsize = rand($size - $size / 10, $size + $size / 10);
$location = $left + ($i * $size + $size / 10);
@imagettftext($this->image, $randsize, rand(- 18, 18), $location, rand($size - $size / 10, $size + $size / 10) + 2, $textColor, $font, $randText);
}
if ($this->showNoisePix == true) {
$this->setNoisePix();
}
if ($this->showNoiseLine == true) {
$this->setNoiseLine();
}
if ($this->showBorder == true) {
$this->borderColor = $this->getColor($this->borderColor);
imageRectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->borderColor);
}
$this->CI->session->set_userdata('auth_code', $code);
ob_clean();
header("Content-type: image/jpeg");
imagejpeg($this->image);
imagedestroy($this->image);
}
/**
* 显示验证码的JS调用
*
*/
function showScript()
{
//显示验证码
echo "var img_src = '/imgauthcode/show/?';\n";
echo "document.writeln('<img id=\"img_authcode\" src=\"' + img_src + Math.random() + '\" style=\"cursor:hand;\" onclick=\"this.src=img_src + Math.random();\" alt=\"点击更换图片\">');";
}
/**
* 检查验证码是否正确
*
* @param string $auth_code
* @return bool
*/
function check($auth_code = null)
{
return ($this->CI->session->userdata('auth_code') && $auth_code) ? ($this->CI->session->userdata('auth_code') === $auth_code) : false;
}
function getColor($color)
{
$color = eregi_replace("^#", "", $color);
$r = $color . $color;
$r = hexdec($r);
$b = $color . $color;
$b = hexdec($b);
$g = $color . $color;
$g = hexdec($g);
$color = imagecolorallocate($this->image, $r, $b, $g);
return $color;
}
function setNoisePix()
{
for($i = 0; $i < $this->noiseNumPix; $i ++) {
$randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imageSetPixel($this->image, rand(0, $this->width), rand(0, $this->height), $randColor);
}
}
function setNoiseLine()
{
for($i = 0; $i < $this->noiseNumLine; $i ++) {
$randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imageline($this->image, rand(1, $this->width), rand(1, $this->height), rand(1, $this->width), rand(1, $this->height), $randColor);
}
}
}
2、创建CI控制器 imgauthcode.php
这个控制器只是示范如何通过CI调用该类库。
<?php
class Imgauthcode extends Controller
{
function __construct()
{
parent::__construct();
$this->load->library('Authcode');
}
/**
* 显示图片
*
*/
function show()
{
$this->authcode->show();
}
/**
* js调用显示图片
*
*/
function show_script()
{
$this->authcode->showScript();
}
/**
* ajax验证
*
*/
function check()
{
if ($this->authcode->check($this->uri->segment(3))) {
$xml_data['result'] = 'succeed';
$this->load->library('My_Xml');
echo XML_serialize($xml_data);
} else {
echo '验证码不正确,请重新输入';
}
}
}
3、上传字体
类库的默认字体路径是:system/application/libraries/fonts,如果需要改变目录,请相应修改类库配置。
4、页面JS调用
这个时候已经可以通过访问地址/imgauthcode/show查看到验证码的图片。
如果需要在模板里面显示验证码,只需要在相应地方插入JS:
<script language="javascript" type="text/javascript" src="/imgauthcode/show_script/"></script> 感谢分享,不错 下下来用一下,谢谢分享。 麻烦给个如何调用呢?
1.控制器放在哪里
2.图片文件夹放在哪里?(我这里显示不出来图片,是否需要建立图片文件夹) 顶起来,希望给个简单的教程! 由于是从自己的blog“转”过来的,教程内容较多,所以希望大家到这个地址去看,http://www.kandejian.com/code/authcode-class-based-on-codeigniter.html
谢谢了:) 源地址打不开了。。。 回复 1# wintion
类库里show()函数里,ob_clean()那句报错:
A PHP Error was encountered
Severity: Notice
Message: ob_clean() : failed to delete buffer. No buffer to delete.
Filename: libraries/Authcode.php
Line Number: 83
把ob_clean()注释掉就正常了,这是什么原因? 谢谢LZ的贡献,很好用。但是发现个BUG,CHECK函数返回后没有销毁SESSION,浏览器返回按钮可以重复使用。
稍微修改下即OK
function check($auth_code = null)
{
if($this->CI->session->userdata('auth_code') && $auth_code){
if($this->CI->session->userdata('auth_code') === $auth_code){
$this->CI->session->unset_userdata('auth_code');
return TRUE;
}
}
return false;
} 为什么一直调用不成功呢郁闷。 :'(我老是不成功