如何使用codeigniter在注册页面上验证验证码?

时间:2017-07-01 09:24:07

标签: php codeigniter

我已经在codeigniter上尝试了很多关于验证验证码的教程,并且适用于我的表单,但我仍然不明白如何验证$ word == $ this-> input-> post('captcha ')然后是真的(验证成功)。

我有一个注册控制器(控制器/ Registration.php)

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');

 class Register extends CI_Controller {

     function __construct(){
         parent::__construct();
         $this->load->library(array('form_validation'));
         $this->load->library('image_lib');
         $this->load->helper(array('url','form','captcha'));
         $this->load->model('m_account'); //call model
     }

     public function index() {
         $dat = array(
            'img_path' => './static/',
            'img_url' => 'http://localhost/ks/static/',
            'img_width' =>'150',
            'img_height' =>'50'
            );
         $captcha = create_captcha($dat);
         $this->form_validation->set_rules('username', 'USERNAME','required');
         $this->form_validation->set_rules('email','EMAIL','required|valid_email');
         $this->form_validation->set_rules('password','PASSWORD','required');
         $this->form_validation->set_rules('password_conf','PASSWORD','required|matches[password]');
         $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha');

         if($this->form_validation->run() == FALSE) {
             $this->load->view('reader');
             $this->load->view('account/register',$captcha);
             $this->load->view('footer');
         }else{

             $data['username'] =    $this->input->post('username');
             $data['email']  =    $this->input->post('email');
             $data['password'] =    md5($this->input->post('password'));

             $this->m_account->daftar($data);

             $pesan['message'] =    "Pendaftaran berhasil anda berhasil silahkan login";
             $this->load->view('reader');
             $this->load->view('account/sukses',$pesan);

             $this->load->view('footer');
             header( "refresh:1;url=login" );
         }
     }
    public function validate_captcha(){
    if($this->input->post('captcha') != $this->$captcha)
    {
        $this->form_validation->set_message('validate_captcha', 'Wrong captcha code');
        return false;
    }else{
        return true;
    }

这是我的表格注册(views / account / register.php):

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');


     <div id="content">
     <h2>Pendaftaran Member Baru</h2>

     <?php echo form_open('register');?>

 <div id=form_wrapper>
 <div class=form_title>Form Pendaftaran</div>
     <div class=form_label>Username</div>
     <div class=form_value><input type="text" maxlength="15" id="user" name="username" class="form_input" autocomplete="off" width="50" value="<?php echo set_value('username'); ?>"/> </div>
     <div class=form_label>Email:</div>
     <div class=form_value><input type="email" maxlength="100" name="email" id="email" class="form_input" autocomplete="off" value="<?php echo set_value('email'); ?>"/></div>
     <p> <?php echo form_error('email'); ?> </p>
     <div class=form_label>Password:</div>
     <div class=form_value><input type="password" id="pass" maxlength="15" name="password" class="form_input" autocomplete="off" value="<?php echo set_value('password'); ?>"/></div>
     <p> <?php echo form_error('password'); ?> </p>
     <div class=form_label>Password Confirm:</div>
     <div class=form_value><input type="password" id="pass" maxlength="15" name="password_conf" class="form_input" autocomplete="off"  value="<?php echo set_value('password_conf'); ?>"/></div>
     <p> <?php echo form_error('password_conf'); ?> </p>

    <p>
    <?php
echo $image;
echo $word;
?>
    <label for="name">Captcha:
      <input id="captcha" name="captcha" type="text" value=""/>
    </label></p>
     <p>
     <input type="submit" name="btnSubmit" value="Daftar" />
     </p>
 </div>
     <?php echo form_close();?>
 </div>
 ?>

1 个答案:

答案 0 :(得分:0)

你应该试试这个验证码。它现在很受欢迎。来自Google的开发者礼物。

Google reCAPTCHA

什么是reCAPTCHA? 易于添加,高级安全性

reCAPTCHA是Google提供的免费服务,可保护您的网站免受垃圾邮件和滥用行为的侵害。它使用先进的风险分析技术来分辨人类和机器人。使用新的API,大量有效的人类用户将无需解决CAPTCHA即可通过reCAPTCHA挑战。 reCAPTCHA以小部件的形式出现,您可以轻松添加到您的博客,论坛,注册表单等。More Info here