Я создал некоторую форму для вставки данных в базу данных и для проверки того, были ли данные отправлены от человека, я использовал CAPTCHA, который уже интегрирован в CI.
Это мой контроллер:
$checkrules = array( 'img_path' => realpath(APPPATH . '../upload/checking/img') . '/', 'img_url' => base_url() . 'upload/checking/img/', 'font_path' => realpath(APPPATH . '../upload/checking/font.ttf'), 'img_width' => 150, 'img_height' => 30, 'expiration' => 7200 ); $check = create_captcha($checkrules); $data['checkimg'] = $check['image']; $this->form_validation->set_rules('name', 'Name', 'required|max_length[40]|xss_clean'); $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email|xss_clean'); $this->form_validation->set_rules('website', 'Website', 'max_length[80]|prep_url|xss_clean'); $this->form_validation->set_rules('comment', 'Comment', 'required|xss_clean'); $this->form_validation->set_rules('check', 'Check', 'required|xss_clean'); if ($this->form_validation->run() == FALSE) { $this->load->view('cms/theme', $data); } else { echo "success"; $this->load->view('cms/theme', $data); }
Теперь мой вопрос – это лучший способ проверить CAPTCHA?
1.) Создание обратного вызова, которое я уже сделал, но возникла проблема, потому что когда я отправляю форму, это ошибка с новым кодом CAPTCHA.
2.) Вставьте код CAPTCHA в базу данных и проверьте его. Проблема в том, что будет много загружаемой базы данных, и она будет очень занята.
И второй вопрос. Это CAPTCHA сохранение только .jpg картинок в папке, или это может быть любой другой формат там? (Я спрашиваю об этом, потому что я хочу удалить эту капчу после того, как они будут использованы.)
* Example of captcha validation without database useage * Instead of it used session to store captcha value * The images will be deleted after the use
public function index() { $this->load->helper(array('form', 'url','captcha')); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha'); if($this->form_validation->run() == FALSE) { $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z')); $original_string = implode("", $original_string); $captcha = substr(str_shuffle($original_string), 0, 6); //Field validation failed. User redirected to login page $vals = array( 'word' => $captcha, 'img_path' => './captcha/', 'img_url' => 'http://mycodeignitor.org/captcha/', 'font_path' => BASEPATH.'fonts/texb.ttf', 'img_width' => 150, 'img_height' => 50, 'expiration' => 7200 ); $cap = create_captcha($vals); $data['image'] = $cap['image']; if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->set_userdata(array('captcha'=>$captcha, 'image' => $cap['time'].'.jpg')); $this->load->view('index_index',$data); } else { if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->unset_userdata('captcha'); $this->session->unset_userdata('image'); redirect('home', 'refresh'); } } public function validate_captcha(){ if($this->input->post('captcha') != $this->session->userdata['captcha']) { $this->form_validation->set_message('validate_captcha', 'Wrong captcha code, hmm are you the Terminator?'); return false; }else{ return true; } }
,public function index() { $this->load->helper(array('form', 'url','captcha')); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha'); if($this->form_validation->run() == FALSE) { $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z')); $original_string = implode("", $original_string); $captcha = substr(str_shuffle($original_string), 0, 6); //Field validation failed. User redirected to login page $vals = array( 'word' => $captcha, 'img_path' => './captcha/', 'img_url' => 'http://mycodeignitor.org/captcha/', 'font_path' => BASEPATH.'fonts/texb.ttf', 'img_width' => 150, 'img_height' => 50, 'expiration' => 7200 ); $cap = create_captcha($vals); $data['image'] = $cap['image']; if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->set_userdata(array('captcha'=>$captcha, 'image' => $cap['time'].'.jpg')); $this->load->view('index_index',$data); } else { if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->unset_userdata('captcha'); $this->session->unset_userdata('image'); redirect('home', 'refresh'); } } public function validate_captcha(){ if($this->input->post('captcha') != $this->session->userdata['captcha']) { $this->form_validation->set_message('validate_captcha', 'Wrong captcha code, hmm are you the Terminator?'); return false; }else{ return true; } }
,public function index() { $this->load->helper(array('form', 'url','captcha')); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha'); if($this->form_validation->run() == FALSE) { $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z')); $original_string = implode("", $original_string); $captcha = substr(str_shuffle($original_string), 0, 6); //Field validation failed. User redirected to login page $vals = array( 'word' => $captcha, 'img_path' => './captcha/', 'img_url' => 'http://mycodeignitor.org/captcha/', 'font_path' => BASEPATH.'fonts/texb.ttf', 'img_width' => 150, 'img_height' => 50, 'expiration' => 7200 ); $cap = create_captcha($vals); $data['image'] = $cap['image']; if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->set_userdata(array('captcha'=>$captcha, 'image' => $cap['time'].'.jpg')); $this->load->view('index_index',$data); } else { if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->unset_userdata('captcha'); $this->session->unset_userdata('image'); redirect('home', 'refresh'); } } public function validate_captcha(){ if($this->input->post('captcha') != $this->session->userdata['captcha']) { $this->form_validation->set_message('validate_captcha', 'Wrong captcha code, hmm are you the Terminator?'); return false; }else{ return true; } }
,public function index() { $this->load->helper(array('form', 'url','captcha')); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha'); if($this->form_validation->run() == FALSE) { $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z')); $original_string = implode("", $original_string); $captcha = substr(str_shuffle($original_string), 0, 6); //Field validation failed. User redirected to login page $vals = array( 'word' => $captcha, 'img_path' => './captcha/', 'img_url' => 'http://mycodeignitor.org/captcha/', 'font_path' => BASEPATH.'fonts/texb.ttf', 'img_width' => 150, 'img_height' => 50, 'expiration' => 7200 ); $cap = create_captcha($vals); $data['image'] = $cap['image']; if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->set_userdata(array('captcha'=>$captcha, 'image' => $cap['time'].'.jpg')); $this->load->view('index_index',$data); } else { if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->unset_userdata('captcha'); $this->session->unset_userdata('image'); redirect('home', 'refresh'); } } public function validate_captcha(){ if($this->input->post('captcha') != $this->session->userdata['captcha']) { $this->form_validation->set_message('validate_captcha', 'Wrong captcha code, hmm are you the Terminator?'); return false; }else{ return true; } }
,public function index() { $this->load->helper(array('form', 'url','captcha')); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha'); if($this->form_validation->run() == FALSE) { $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z')); $original_string = implode("", $original_string); $captcha = substr(str_shuffle($original_string), 0, 6); //Field validation failed. User redirected to login page $vals = array( 'word' => $captcha, 'img_path' => './captcha/', 'img_url' => 'http://mycodeignitor.org/captcha/', 'font_path' => BASEPATH.'fonts/texb.ttf', 'img_width' => 150, 'img_height' => 50, 'expiration' => 7200 ); $cap = create_captcha($vals); $data['image'] = $cap['image']; if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->set_userdata(array('captcha'=>$captcha, 'image' => $cap['time'].'.jpg')); $this->load->view('index_index',$data); } else { if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->unset_userdata('captcha'); $this->session->unset_userdata('image'); redirect('home', 'refresh'); } } public function validate_captcha(){ if($this->input->post('captcha') != $this->session->userdata['captcha']) { $this->form_validation->set_message('validate_captcha', 'Wrong captcha code, hmm are you the Terminator?'); return false; }else{ return true; } }
,public function index() { $this->load->helper(array('form', 'url','captcha')); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha'); if($this->form_validation->run() == FALSE) { $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z')); $original_string = implode("", $original_string); $captcha = substr(str_shuffle($original_string), 0, 6); //Field validation failed. User redirected to login page $vals = array( 'word' => $captcha, 'img_path' => './captcha/', 'img_url' => 'http://mycodeignitor.org/captcha/', 'font_path' => BASEPATH.'fonts/texb.ttf', 'img_width' => 150, 'img_height' => 50, 'expiration' => 7200 ); $cap = create_captcha($vals); $data['image'] = $cap['image']; if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->set_userdata(array('captcha'=>$captcha, 'image' => $cap['time'].'.jpg')); $this->load->view('index_index',$data); } else { if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image'])) unlink(BASEPATH."../captcha/".$this->session->userdata['image']); $this->session->unset_userdata('captcha'); $this->session->unset_userdata('image'); redirect('home', 'refresh'); } } public function validate_captcha(){ if($this->input->post('captcha') != $this->session->userdata['captcha']) { $this->form_validation->set_message('validate_captcha', 'Wrong captcha code, hmm are you the Terminator?'); return false; }else{ return true; } }
Я предлагаю вам использовать recaptcha, который легко реализовать в codeigniter: http://codeigniter.com/wiki/ReCAPTCHA