本帖最后由 一迢 于 2012-10-19 19:37 编辑
写的验证码类如下,放入了libraryies中
PHP复制代码
class Captcha {
private $chars = '';
private $code = '';
private $img = null;
private $font = null;
private $fontsize = 13;
private $fontcolor = null;
private $ci = null;
public $length = 4;
public $width = 145;
public $height = 20;
public function __construct (){
$this->chars = '0123456789';
$this->font = FCPATH .'source/font/Consola.ttf';
$this->ci = & get_instance ();
}
/*随机生成验证码,并写入session*/
private function createCode (){
$length = strlen($this->chars) - 1;
$code = '';
for($i=0;$i<$this->length;$i++){
$code .= $this->chars[mt_rand(0,$length)];
}
$this->code = $code;
$this->ci->load->library('session');
$this->ci->session->set_userdata(array('captcha' => $this->code));
}
/*生成背景*/
private function createBg (){
$this->img = imagecreatefromjpeg(FCPATH .'/source/codebg/background'.mt_rand(1,8).'.jpg');
}
/*写入验证码*/
private function writeCode (){
$x = $this->width / $this->length;
for($i = 0;$i < $this->length;$i++){
$this->fongcolor = imagecolorallocate($this->img,mt_rand(0,55),mt_rand(0,55),mt_rand(0,55));
imagettftext($this->img,$this->fontsize,0,$x*$i/4 + $this->width/2.6,16,$this->fongcolor,$this->font,$this->code[$i]);
//imagestring($this->img,4,($x*$i/4 + $this->width/2.6),2, $this->code[$i], $this->fontcolor);
}
}
/*生成验证码图片*/
private function createCodeImg (){
header("Pragma:no-cache");
header("Cache-control:no-cache");
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
/*前端显示验证码图片*/
public function showcode (){
$this->createCode();
$this->createBg();
$this->writeCode();
$this->createCodeImg();
}
}
复制代码
调用:
PHP复制代码
class test extends FR_Controller {
private $temp = '';
function __construct (){
parent ::__construct ();
}
function index (){
echo '<img src="'.site_url ('test/captcha').'" alt="验证码" /><br/>';
echo $this->session->userdata('captcha');
//$this->captcha->showcaptcha();
}
function captcha (){
$this->load->library('captcha');
$this->captcha->showcode();
}
function date_time (){
$this->load->helper(array('time'));
$date = getWeek ('2012-09-23');
echo date('Y-m-d',$date['start']);
}
}
复制代码
但是我的session总是慢一步啊!一直没想明白……
|