hey guys im trying to validate a captcha code in codeigniter. is there an easy way to validate the captcha? here is my code and it is not working because everytime it loads the page it will call a new captcha which will replace the word in the session.
public function index() {
$originalString = array_merge(range(0,9), range('a','z'), range('A', 'Z'));
$originalString = implode("", $originalString);
$captcha = substr(str_shuffle($originalString), 0, 6);
$vals = array(
'word' => $captcha,
'img_path' => './captcha/',
'img_url' => base_url().'captcha/',
'font_path' => './system/fonts/texb.ttf',
'img_width' => 150,
'img_height' => 50,
'expiration' => 7200 );
$cap = create_captcha($vals);
$captchaImage = $cap['image'];
$this->session->set_userdata($cap);
if(isset($_POST['register'])) {
$this->form_validation->set_rules('firstName', 'First Name', 'required');
$this->form_validation->set_rules('lastName', 'Last Name', 'required');
$this->form_validation->set_rules('emailAddress', 'Email', 'required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[6]');
$this->form_validation->set_rules('password', 'Password', 'required|matches[confirm-password]|min_length[6]');
$this->form_validation->set_rules('confirm-password', 'Password Confirm', 'required');
$this->form_validation->set_rules('secretquestion', 'Secret Question', 'required');
$this->form_validation->set_rules('answer', 'Answer', 'required');
$this->form_validation->set_rules('inputCode', 'Captcha', 'required|');
if ($this->form_validation->run() == TRUE) {
$user = $this->Account_Model->validation($_POST['username']);
if($_POST['inputCode'] != $this->session->userdata('word')){
echo $_POST['inputCode'] .' = ' .$this->session->userdata('word');
$this->_error = 'Code does not match the image';
} else {
if(empty($user)) {
$this->load->library('upload');
$accountId = $this->Account_Model->addUser($_POST);
$config['upload_path'] = './assets/uploads/avatars';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $_POST['username'];
$this->upload->initialize($config);
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('avatar'))
{
$error = array('error' => $this->upload->display_errors());
}
if($accountId){
$this->_body = array(
'username' => $_POST['username'],
'email' => $_POST['emailAddress'],
'secretQuestion' => $_POST['secretquestion'],
'answer' => $_POST['answer'],
'captchaImage' => $captchaImage
);
$this->_template = 'register_success';
return $this->_renderPage();
}
}
}
}
}
$this->_body = array(
'secretQuestions' => $this->questions,
'captchaImage' => $captchaImage,
'error' => $this->_error
);
return $this->_renderPage();
}
is there a better way to do this??.. please help.. tnx in advanced..