打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
验证码模型 For CodeIgniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* by frank
* qq 108818
**/


Class Vcode_model extends Model
{
public $width = 57; //宽度
public $height = 20; //高度
public $len = 4; //字符长度
public $backcolor = '#FFFFFF' ; //背景色
public $bordercolor = null; //边框色
public $noisenum = NULL ; //杂点数量

public $textsize = 10; //字体大小
public $font = "04b_08.ttf" ; //自定义字体
public $imagename ;
protected $image ; //图片变量
protected $backcolorRGB ; //背景色RGB
protected $bordercolorRGB = null; //边框色
protected $size; //每个字符的宽度(px)
protected $sizestr2str; //字符间距
public $vcode = NULL; //验证码内容(数字)
function __construct()
{
parent::Model();


}
public function show_img()
{
header("Content-type: image/png");
//$this->imagename = 'vcode/'.md5(date("YmdHis").$this->vcode).'.png';
//imagePng($this->image,$this->imagename);
imagePng($this->image);
imagedestroy($this->image);
}

public function make_img()
{
$this->image = imageCreate($this->width, $this->height); //创建图片
$this->backcolorRGB = $this->getcolor($this->backcolor); //将#ffffff格式的背景色转换成RGB格式
imageFilledRectangle($this->image, 0, 0, $this->width, $this->height, $this->backcolorRGB); //画一矩形 并填充
$this->size = $this->width/$this->len; //宽度除以字符数 = 每个字符需要的宽度
if($this->size>$this->height) $this->size=$this->height; //如果 每个字符需要的宽度 大于图片高度 则 单字符宽度=高度(正方块)
$this->sizestr2str = $this->size/10 ; //以每个字符的1/10宽度为 字符间距
$left = ($this->width-$this->len*($this->size+$this->sizestr2str))/$this->size; // (验证码图片宽度 - 实际需要的宽度)/每个字符的宽度 = 距离左边的宽度
for ($i=0; $i<$this->len; $i++)
{
$randtext = rand(0, 9); //验证码数字 0-9随机数
$this->vcode .= $randtext; //写入session的数字
$textColor = imageColorAllocate($this->image, rand(50, 155), rand(50, 155), rand(50, 155)); //图片文字颜色
if (!isset($this->textsize) ) $this->textsize = rand( ($this->size-$this->size/10), ($this->size + $this->size/10) ); //如果未定义字体大小 则取随机大小
$location = $left + ($i*$this->size+$this->size/10);
imagettftext($this->image, $this->textsize, rand(-18,18), $location, rand($this->size-$this->size/10, $this->size+$this->size/10), $textColor, $this->font, $randtext); //生成单个字体图象
}
if( isset($this->noisenum)) $this->setnoise(); //杂点处理
$_SESSION['vcode'] = $this->vcode; //写入session

if(isset($this->bordercolor)) //边框处理
{
$this->bordercolorRGB = $this->getcolor($this->bordercolor);
imageRectangle($this->image, 0, 0, $this->width-1, $this->height-1, $this->bordercolorRGB);
}

}

protected function getcolor($color)
{
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($this->image, $r, $b, $g);
return $color;
}
protected function setnoise()
{
for ($i=0; $i<$this->noisenum; $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);
}
}
}
复制代码在 system\application\controllers 下建立 文件 vcode.php

内容如下
PHP

<?php
/**
* by frank
* qq 108818
**/

class Index extends Controller {
function __construct()
{
parent::Controller();
$this->load->helper('url');
$this->load->library('session');
$this->vcodeimg();

function vcodeimg()
{
/***验证码**/
$this->load->model('vcode_model');
$this->vcode_model->make_img();
$this->session->set_userdata('vcode', $this->vcode_model->vcode);
$this->vcode_model->show_img();
}

function showsession() //这个方法可以删除 仅供debug
{
echo '<pre>';
var_dump($this->session->userdata);
echo '</pre>';
}

}
复制代码
然后将打开

http://127.0.0.1/index.php/vcode/

看看能否正确看到验证码

需要查看 当前session中保存的数据 访问这个页面
http://127.0.0.1/index.php/vcode/showsession数组中 vcode 下面的4位 阿拉伯数字 即为验证码


如果想要在某个页面显示验证码图片
需要 在该页面所对应的视图中 增加如下代码片段
JS
function $(objectId)
{
if(document.getElementById && document.getElementById(objectId))
{
// W3C DOM
return document.getElementById(objectId);
}
else if (document.all && document.all(objectId))
{
// MSIE 4 DOM
return document.all(objectId);
}
else if (document.layers && document.layers[objectId])
{
// NN 4 DOM.. note: this won't find nested layers
return document.layers[objectId];
}
else
{
alert('取值失败未找到对应id');
}
}

function reloadvcode(obj)
{
$(obj).src="/index.php/vcode/"+Math.round(Math.random()*100000);
}
复制代码
HTML
<img src="/index.php/vcode" style="vertical-align:middle;" width="57" height="20" id="vcodeimg" title="单击刷新" />
复制代码


如何验证?

比如您引用验证码的表单页面 action="/index.php/contrl/func/"

那么 在 contrl.php 这个控制器文件中的func 方法中
加入以下代码
PHP
$this->load->helper('url');
$this->load->library('session');
if ($this->session->userdata['vcode'] != $_POST['vcode'] )
{
show_error ('Bad news: 验证码输入不正确!');
}
else //校验成功
{
//your code.....
}
复制代码
祝您好运

/////////////////////////////////
补充点 config.php 里面的
$config['sess_time_to_update']
根据自己的实际情况设置 否则session 无法更新

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
最新土豆视频站外无广告无弹窗调用代码
解析PHP计算页面执行时间的实现代码
php绘制图片验证码实例
如何让Wordpress模板在不同页面显示不同侧边栏
PHP设计模式—观察者模式
一起搞懂PHP的错误和异常(二)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服