CI中的验证码(十八)

CI中的验证码(十八)

1,先创建验证码类文件

application/controllers/user.php

//验证码
	public function yzm(){
		$this->load->helper('captcha');
		$vals = array(
				//'word'=>'Random word',
				'img_path'=>'./captcha/',
				'img_url'=>'http://example.com/captcha/',
				//'font_path'=>'./path/to/fonts/texb.ttf',
				//'img_width'=>'150',
				//'img_height'=>30,
				//'expiration'=>7200
		);
		$cap = create_captcha($vals);
		echo $cap['image'];
	}

然后根据配置在CI根目录创建文件夹

captcha

blob.png

访问:index.php/user/yzm

blob.png

我们可以看到CI会为我们创建一个验证码图片

我们再把验证码赋给模板中

application/controllers/user.php

        //验证码
	public function yzm(){
		$this->load->helper('captcha');
		$vals = array(
				//'word'=>'Random word',//验证码格式,使用自己生成的随机字符串,如果是中文需要自己指定字体,font_path
				'img_path'=>'./captcha/',//此目录需要手动创建
				'img_url'=>base_url().'captcha/',
				//'font_path'=>'./path/to/fonts/texb.ttf',
				'img_width'=>'100',
				'img_height'=>30,
				'expiration'=>5//过期时间,时间一到,会自动删除图片
		);
		$cap = create_captcha($vals);
		$this->load->view('user/yzm',array('cap'=>$cap['image']));
		//echo $cap['image'];
	}

新建视图文件

application/views/yzm.php

<html>
<head></head>
<body>
<input type=""><?=$cap?>
</body>
</html>

访问:index.php/user/yzm

blob.png

再去查看CI根目录captcha生成的验证码图片

blob.png


2,如何验证图片中的数字呢?

$cap['word']是获取验证码中的数字,官方手册是把验证码存入到数据库中,不推荐,我们还是把验证码放入到session中

                //验证码放入session
		session_start();
		$_SESSION['cap']=$cap['word'];
		//验证时,对比$_SESSION['cap'];


回复列表


回复操作