Skye_k 发表于 2017-1-24 16:40:25

CI实现验证码的问题

想实现一个验证码功能
Controller:
<?php
/**
*验证登录的控制器
*
*/
    class Login extends CI_Controller
    {
      public function __construct(){
            parent::__construct();
            $this->load->model('login_model');
      }

      public function login(){
            $imgVcode = $this->login_model->getVcode();
            $img = $imgVcode['image'];
            return $img;
      }
    }
?>
Model

<?php
/**
* 登陆验证的model
*/
class Login_model extends CI_Model{
    public function __construct(){
      $this->load->helper("captcha");
      $this->load->database();
    }

    public function getVcode(){
      $vals = array(
            'word' => rand(10000,100000),//验证码上显示的字符
            'img_path' => './captcha/',//验证码保存路径
            'img_url' => 'http://localhost/captcha/', //验证码图片url
            'font_path' => './path/to/fonts/texb.ttf',//验证码上字体
            'img_width' => '100',
            'img_height' => 25,
            'expiration' => 100,
            'word_length' => 8,
            'font_size' => 16,
            'img_id' => 'Imageid',
            'pool' => '23456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            'color' => array(
                'background' => array(255,255,255),
                'border' => array(255,255,255),
                'text' => array(0,0,0),
                'grid' => array(255,40,40)
            )
      );

      $cap = create_captcha($vals);
      return $cap;
    }
}
?>



页面:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="utf-8">
    <title>登陆</title>
</head>
<body>
<p style="font-size: 40px" align="center">欢迎来到BBS</p>
<form action="validate.php" method="post" name="form">
    <center>

      用户名:<input type="text" name="username" style="width: 150px" >
      <br><br>
      密&nbsp;码:<input type="password" name="password" style="width: 150px">
      <br><br>
      验证码:<input type="text" name="validate" style="width: 100px">
      <img src="<?php echo site_url('login/login'); ?>" alt="" onclick=this.src="<?php
      echo site_url('login/login').'/' ?>"+Math.random() style="cursor:pointer;" title="看不清?再来一张吧"/>

      <br><br>
      <input type="submit" value="提交" style="width: 100px">
      &nbsp;
      <input type="reset" value="重置" style="width: 100px">
    </center>
</form>
</body>
<htfile:///C:\Users\ADMINI~1\AppData\Local\Temp\@IR3P(8S$C$Z$TY~5I{QEPC.gifml>




图片显示不出来,直接访问localhost/index.php?/login/login 是没有问题的,可以正常显示图片
需要怎么做?
谢谢!

Hex 发表于 2017-1-24 17:07:15

楼主的验证码 helper 用法是错误的,create_captcha($vals); 返回的是一个 HTML 代码,而不是图片数据。

Skye_k 发表于 2017-1-24 19:52:44

Hex 发表于 2017-1-24 17:07
楼主的验证码 helper 用法是错误的,create_captcha($vals); 返回的是一个 HTML 代码,而不是图片数据。 ...

好的好的,在qq上沟通过了,已经解决了

88646958@qq.com 发表于 2017-1-28 15:26:40

自带的验证码辅助函数返回的是一个html数据,如果想用图片数据的话可以修改扩展下,可以看看这篇帖子
http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=23809&page=1#pid100584
页: [1]
查看完整版本: CI实现验证码的问题