2013-11-25 4 views
0

эй, ребята, я пытаюсь проверить код captcha в codeigniter. есть ли простой способ проверить капчу? вот мой код, и он не работает, потому что каждый раз, когда он загружает страницу, он будет вызывать новую капчу, которая заменит слово в сеансе.проверка правильности кода captchaign

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(); 
} 

есть лучший способ сделать это ?? .. помогите пожалуйста .. Тпх в передовой ..

ответ

1

Может быть, самый простой способ был бы не создавать новую капчу, как только пользователь отправляет форму , или, по крайней мере, не обновлять сеанс.

if(!$_POST){ 
$this->session->set_userdata($cap); 
} 
Смежные вопросы