Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- User.php
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class User extends CI_Controller{
- public function check_captcha($captcha_input)
- {
- // First, delete old captchas
- $expiration = time() - 7200; // Two hour limit
- $this->db->where('captcha_time < ', $expiration)
- ->delete('captcha');
- // Then see if a captcha exists:
- $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
- $binds = array($captcha_input, $this->input->ip_address(), $expiration);
- $query = $this->db->query($sql, $binds);
- $row = $query->row();
- if ($row->count == 0){
- $this->form_validation->set_message('check_captcha', 'Captcha Fail');
- return FALSE;
- }else {
- return TRUE;
- }
- }
- public function register(){
- $this->form_validation->set_rules('user_name' , 'Username' , 'trim|required|max_length[32]');
- $this->form_validation->set_rules('captcha' , 'Captcha' , 'trim|required|max_length[32]|callback_check_captcha');
- $this->form_validation->set_rules('password', 'Password','trim|required|min_length[5]|max_length[12]');
- $this->form_validation->set_rules('confirm_password', 'Confirm Password','trim|required|min_length[5]|max_length[12]|matches[password]');
- $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
- if ($this->form_validation->run()==FALSE){
- //Captcha code
- $this->load->helper('captcha');
- //Captcha Config
- $vals = array(
- 'img_path' => './captcha',
- 'img_url' => './captcha/',
- 'img_width' => '150',
- 'img_height' => 30,
- 'expiration' => 7200,
- 'word_length' => 8,
- 'font_size' => 16,
- 'img_id' => 'Imageid',
- 'font_path' => '/fonts/Open.ttf',
- // White background and border, black text and red grid
- 'colors' => array(
- 'background' => array(255, 255, 255),
- 'border' => array(255, 255, 255),
- 'text' => array(0, 0, 0),
- 'grid' => array(255, 40, 40)
- )
- );
- //Create the Captcha
- $cap = create_captcha($vals);
- // Store The Created Captcha in DB for Verification
- $data = array(
- 'captcha_time' => $cap['time'],
- 'ip_address' => $this->input->ip_address(),
- 'word' => $cap['word']
- );
- $this->register_model->store_captcha($data);
- //Load View
- $data['image']=$cap['image'];
- var_dump($data['image']);exit();
- $this->load->view('templates/header');
- $this->load->view('register' , $data);
- $this->load->view('templates/footer');
- }else {
- $option=array('cost'=>12);
- $encrypt_password=password_hash($this->input->post('password'),PASSWORD_BCRYPT,$option);
- $this->register_model->add_user($encrypt_password);
- $this->session->set_flashdata('user_registered','You are Successfully Registered and can Log in ');
- redirect('register/success');
- }
- }
- public function success(){
- $this->load->view('success');
- }
- }
- View (register.php)
- <div class="col-md-8">
- <h2>Register</h2><br>
- <?php echo validation_errors(); ?>
- <?php echo form_open("register");
- ?>
- <div class="form-group">
- <label>Enter UserID</label>
- <input type="text" class="form-control" name="user_name" placeholder="Enter Your UserId">
- </div>
- <div class="form-group">
- <label>Enter Your Email</label>
- <input type="email" class="form-control" name="email" placeholder="Enter Your Email">
- </div>
- <div class="form-group">
- <label>Enter Your Password </label>
- <input type="password" class="form-control" name="password" placeholder="Enter Password">
- </div>
- <div class="form-group">
- <label>Re-enter Password </label>
- <input type="password" class="form-control" name="confirm_password" placeholder="Renter Password">
- </div>
- <?php echo $image ; ?>
- <div class="form-group">
- <label>Solve Captcha </label>
- <input type="text" class="form-control" name="captcha" >
- </div>
- <button type="submit" class="btn btn-primary">Submit</button>
- <?php echo form_close();?>
- </div>
Add Comment
Please, Sign In to add comment