duanjiao4763 2018-11-08 07:43
浏览 95
已采纳

Codeigniter - 如何进行计算验证码

I wanted to make a calculation captcha, my current captcha is only a text&number input captcha but it is working just that this captcha is kind of hard so i was thinking to make the captcha easier for login, since the view is like this(the image here) which is making the login harder (ignore the userdata for login and focus the line which has captcha on it and please tell me if the code is too long i will try to make it shorter)

Controller

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";
            }
    }
}

view

<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条回答 默认 最新

  • duanjiani6826 2018-11-08 10:04
    关注

    You can make a variable 'word_length' => $var and on reCaptcha use a smaller number.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看