【问题标题】:How to use captcha in CodeIgniter?如何在 CodeIgniter 中使用验证码?
【发布时间】:2015-10-05 21:36:00
【问题描述】:

我遇到了一个我无法弄清楚的小问题。 我的验证码想要没有出现。 我有默认图片的图片。

我的控制器:

 class Captcha extends CI_Controller {

  public function index() {
    // loading captcha helper
    $this->load->helper('captcha');
    //validating form fields
    $this->form_validation->set_rules('userCaptcha', 'Captcha', 'required|callback_check_captcha');
    $userCaptcha = $this->input->post('userCaptcha');
    if ($this->form_validation->run() == false){
      // numeric random number for captcha
      $random_number = substr(number_format(time() * rand(),0,'',''),0,6);
      // setting up captcha config
      $vals = array(
             'word' => $random_number,
             'img_path' => './captcha/',
             'img_url' => base_url().'captcha/',
             'font_path' => './path/to/fonts/texb.ttf',
             'img_width' => 140,
             'img_height' => 32,
             'expiration' => 7200
            );
      $data['captcha'] = create_captcha($vals);
      $this->session->set_userdata('captchaWord',$data['captcha']['word']);
      $this->load->view('captcha', $data);
    }
}

我的看法:

<html>
    <head>
        <title>Adding a Captcha!</title>
    </head>
    <body>
        <h1>Captcha Example</h1>
        <?php echo form_open('captcha'); ?>
        <div class="formSignIn" >
            <div class="form-group">
                 <input autocomplete="off" type="text" id="username" name="username" placeholder="User Email" value="<?php if(!empty($username)){ echo $username;} ?>" />
                  <span class="required-server"><?php echo form_error('username','<p style="color:#F83A18">','</p>'); ?></span> 
            </div>
            <div class="form-group">
                   <input autocomplete="off" type="password" id="user_password" name="user_password" placeholder="User Password" value="" />
                   <span class="required-server"><?php echo form_error('user_password','<p style="color:#F83A18">','</p>'); ?></span>           
           </div>
          <div class="form-group">
                <label for="captcha"><?php echo $captcha['image']; ?></label>
                <br>
                 <input type="text" autocomplete="off" name="userCaptcha" placeholder="Enter above text" value="<?php if(!empty($userCaptcha)){ echo $userCaptcha;} ?>" />
                <span class="required-server"><?php echo form_error('userCaptcha','<p style="color:#F83A18">','</p>'); ?></span> 
           </div>
          <div class="form-group">
               <input type="submit" class="btn btn-success" value="Sign In" name="" />
        </div>
     </div>
     <?php echo form_close(); ?>
</body>
</html>

我放了GD图片库。 我的“验证码”文件夹是可写的。

感谢您的帮助。

【问题讨论】:

    标签: php codeigniter captcha


    【解决方案1】:

    也加载这些库(在__constructor

    $this->load->library('form_validation');
    $this->load->driver("session");
    $this->load->helper(array('form', 'url', 'captcha'));
    

    并且在 Controller 中也添加这个

      // setting up captcha config
      $vals = array(
             'word' => $random_number,
             'img_path' => './captcha/',
             'img_url' => base_url().'captcha/',
             'img_width' => 140,
             'img_height' => 32,
             'expiration' => 7200
            );
      $data['captcha'] = create_captcha($vals);
    

    在表格中

    <?php echo form_open('captcha'); ?>
    <div class="formSignIn" >
      <div class="form-group">
        <input autocomplete="off" type="text" id="username" name="username" placeholder="User Email" value="<?php if(!empty($username)){ echo $username;} ?>" />
        <span class="required-server"><?php echo form_error('username','<p style="color:#F83A18">','</p>'); ?></span> </div>
      <div class="form-group">
        <input autocomplete="off" type="password" id="user_password" name="user_password" placeholder="User Password" value="" />
        <span class="required-server"><?php echo form_error('user_password','<p style="color:#F83A18">','</p>'); ?></span> </div>
      <div class="form-group">
        <label for="captcha"><?php echo $captcha['image']; ?></label>
        <br>
        <input type="text" autocomplete="off" name="userCaptcha" placeholder="Enter above text" value="<?php if(!empty($userCaptcha)){ echo $userCaptcha;} ?>" />
        <span class="required-server"><?php echo form_error('userCaptcha','<p style="color:#F83A18">','</p>'); ?></span> </div>
      <div class="form-group">
        <input type="submit" class="btn btn-success" value="Sign In" name="" />
      </div>
    </div>
    <?php echo form_close(); ?>
    

    所以它看起来像

    Refer this link too

    【讨论】:

      【解决方案2】:

      您必须将验证码的图像放在您的查看页面中:

      <label for="captcha"><img src="<?php echo base_url();?>uploads/captcha/<?php echo $captcha['image']';?>" width="160" height="40" /></label>
      

      在上面的行中,给出您上传的验证码图像的适当路径。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-21
        • 2012-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-03
        • 1970-01-01
        相关资源
        最近更新 更多