Codeigniter-如何制作验证码

时间:2018-11-08 07:43:12

标签: php codeigniter captcha

我想进行一个计算验证码,我目前的验证码只是一个文本和数字输入的验证码,但它的工作原理是该验证码有点难,所以我想让验证码更易于登录,因为视图就像{ {3}}使得登录更加困难(忽略登录的用户数据,并在上面包含验证码的行,请告诉我代码是否太长,我将尝试使其更短)

控制器

public function index(){
        $config = array(
            'img_path'      => 'captcha_images/',
            'img_url'       => base_url().'captcha_images/',
            'img_width'     => '150',
            'img_height'    => 50,
            'word_length'   => 8,
            'font_size'     => 16
        );
        $captcha = create_captcha($config);

        // Unset previous captcha and store new captcha word
        $this->session->unset_userdata('captchaCode');
        $this->session->set_userdata('captchaCode',$captcha['word']);

        // Send captcha image to view
        $data['captchaImg'] = $captcha['image'];

        $data['tampilan'] = $this->m_model->a('judul')->result();
        $this->load->view('login',$data);
    }

    function aksi_login(){
        $data = array('username' => $this->input->post('username', TRUE),
                        'password' => md5($this->input->post('password', TRUE))
        );


        $this->load->model('m_model'); // load model_user
        $hasil = $this->m_model->cek_user($data);
        if ($hasil->num_rows() == 1 && $this->input->post('submit')){
            $inputCaptcha = $this->input->post('captcha');
            $sessCaptcha = $this->session->userdata('captchaCode');
            if($inputCaptcha === $sessCaptcha){
            foreach ($hasil->result() as $sess) {
                $sess_data['logged_in'] = 'Sudah Login';
                $sess_data['id_user'] = $sess->uid;
                $sess_data['username'] = $sess->username;
                $sess_data['level'] = $sess->level;
                $this->session->set_userdata($sess_data);
            }


            if ($this->session->userdata('level')=='1') {
                redirect('admin');
            }
            elseif ($this->session->userdata('level')=='2') {
                redirect('guru');
            }       
        else {
            redirect('welcome/salah');
        }
    }
    else{
                echo "captcha is wrong";
            }
    }
}

视图

<form action="<?php echo base_url()?>welcome/aksi_login" method="post" class="form">
  <h4> Login </h4>

    <inputtype="text" name="username" placeholder="username"/>

    <input name="password" type="password" placeholder="Password"/>


  <a href="<?php echo base_url()?>Welcome" class="refreshCaptcha" ><img src="<?php echo base_url().'asset/foto/refresh.png'; ?>"/></a>
  <p id="captImg"><?php echo $captchaImg; ?></p>
    <inputtype="text" name="captcha" placeholder="Captcha" />


  <input type="submit" name="submit" value="Log in"/>

</form>

1 个答案:

答案 0 :(得分:0)

您可以设置变量'word_length' => $var,并在reCaptcha上使用较小的数字。