|
PHP复制代码 <?php
class Show extends CI_Controller {
function __construct () {
parent :: __construct ();
header("Content-type: text/html; charset=utf-8");//设置页面为utf-8编码
$this -> load -> database(); //载入数据库配置
$this -> load -> helper('url'); //载入url辅助函数
$this -> load -> helper('captcha');//载入验证码辅助函数
$this -> load -> helper('form');//载入表单辅助函数
}
function index () {
//设置验证码参数
$vals = array(
'img_path' => './captcha/', //验证码图片保存路径
'img_url' => 'http://localhost/ci/captcha/', //验证码显示路径
'img_width' => '150', //验证码图片宽度
'expiration' => 120 //验证码保存时长,超时自动销毁
);
$cap = create_captcha ($vals);
$this -> db -> query("DELETE FROM ci_captcha WHERE ip_address ='" . $this -> input -> ip_address() ."'"); //删除和本机IP相同验证码
$data = array('captcha_time' => $cap['time'],
'ip_address' => $this -> input -> ip_address(),
'word' => $cap['word']
);
$query = $this -> db -> insert_string('captcha', $data);
$this -> db -> query($query); //验证码信息入库
// 输出页面
echo 'Submit the word you see below:';
echo $cap['image'];//显示验证码
//开始生成表单
echo form_open ('show/run');
echo form_input ('captcha', '');
echo form_submit ('mysubmit', '提交验证码!');
echo form_close ();
}
function run () {
// 首先删除旧的验证码
$expiration = time()-120; // 2分钟限制
$this -> db -> query("DELETE FROM ci_captcha WHERE captcha_time < " . $expiration);
// 然后再看是否有验证码存在
$sql = "SELECT COUNT(*) AS count FROM ci_captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
$binds = array($_POST['captcha'], $this -> input -> ip_address(), $expiration);
$query = $this -> db -> query($sql, $binds);
$row = $query -> row();
if ($row -> count == 0) {
echo "验证码输入错误或验证码已过期!";
echo anchor ('show/', '返回验证码页面', 'title="返回验证码页面!"');
} else{
echo"验证码正确!";
echo anchor ('show/', '返回验证码页面', 'title="返回验证码页面!"');
}
}
}
复制代码 |
|