1
我已經嘗試了很多有關驗證驗證captcha與適合我的形式,但我仍然不明白如何驗證當$ word == $ this-> input-> post('驗證碼')然後驗證將是真實的(驗證成功)。如何使用codeigniter驗證註冊頁面上的驗證碼?
我有一個登記控制器波紋管(控制器/ 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;
}
這是我的形式註冊(意見/帳號/ 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>
?>
感謝您回覆我的問題。但我仍然想在codeigniter上使用集成的驗證碼,因爲我仍然在學習如何使用codeigniter創建註冊表單。 – hendradev
你得到了什麼錯誤? –
驗證碼驗證,我無法創建正確的代碼。變量$ word是驗證碼值。我想做驗證,如果$ this-> input-> post('captcha')== $ word那麼...... 關於這個函數: public function validate_captcha(){ if($這個 - > input-> post('captcha')!= $ this - > $ captcha) {this-> form_validation-> set_message('validate_captcha','錯誤驗證碼'); 返回false; } else { return true; } – hendradev