php - User activation registration with CodeIgniter -


i'm trying build registration system codeigniter. have controller called user following code:

class user extends ci_controller{     public function __construct()     {         parent::__construct();         $this->load->model('user_model');     }     public function index()     {         if(($this->session->userdata('user_name')!=""))         {             $this->welcome();         }         else{             $data['title']= 'home';             $this->load->view('header_view',$data);             $this->load->view("registration_view.php", $data);             $this->load->view('footer_view',$data);         }     }     public function welcome()     {         $data['title']= 'welcome';         $this->load->view('header_view',$data);         $this->load->view('welcome_view.php', $data);         $this->load->view('footer_view',$data);     }     public function login()     {         $email=$this->input->post('email');         $password=md5($this->input->post('pass'));          $result=$this->user_model->login($email,$password);         if($result) $this->welcome();         else        $this->index();     }     public function thank()     {         $data['title']= 'thank';         $this->load->view('header_view',$data);         $this->load->view('thank_view.php', $data);         $this->load->view('footer_view',$data);     }     public function registration()     {         $this->load->library('form_validation');         // field name, error message, validation rules         $this->form_validation->set_rules('user_name', 'user name', 'trim|required|min_length[4]|xss_clean');         $this->form_validation->set_rules('email_address', 'your email', 'trim|required|valid_email');         $this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[32]');         $this->form_validation->set_rules('con_password', 'password confirmation', 'trim|required|matches[password]');          if($this->form_validation->run() == false)         {             $this->index();         }         else         {             $this->user_model->add_user();             $this->thank();         }     }     public function logout()     {         $newdata = array(         'user_id'   =>'',         'user_name'  =>'',         'user_email'     => '',         'logged_in' => false,         );         $this->session->unset_userdata($newdata );         $this->session->sess_destroy();         $this->index();     } } 

so far good. if go register page registration form displayed. if send form , passes form validation checks, success page, if form has errors, form error messages.

now want database stuff. have idea of how can post values registration form database, no clue how can check if username or email exists, , if so, display error on registration form. , when register in form, how can send activation user email active account in site.

here's registration form view:

<div id="content"> <div class="signup_wrap"> <div class="signin_form">     <?php echo form_open("user/login"); ?>         <label for="email">email:</label>         <input type="text" id="email" name="email" value="" />         <label for="pass">password:</label>         <input type="password" id="pass" name="pass" value="" />         <input type="submit" class="" value="sign in" />     <?php echo form_close(); ?> </div><!--<div class="signin_form">--> </div><!--<div class="signup_wrap">--> <div class="reg_form"> <div class="form_title">sign up</div> <div class="form_sub_title">it's free , can join</div> <?php echo validation_errors('<p class="error">'); ?>     <?php echo form_open("user/registration"); ?>         <p>             <label for="user_name">user name:</label>             <input type="text" id="user_name" name="user_name" value="<?php echo set_value('user_name'); ?>" />         </p>                 <p>             <label for="email_address">your email:</label>             <input type="text" id="email_address" name="email_address" value="<?php echo set_value('email_address'); ?>" />         </p>         <p>             <label for="password">password:</label>             <input type="password" id="password" name="password" value="<?php echo set_value('password'); ?>" />         </p>         <p>             <label for="con_password">confirm password:</label>             <input type="password" id="con_password" name="con_password" value="<?php echo set_value('con_password'); ?>" />         </p>                 <p>             <input type="submit" class="greenbutton" value="submit" />         </p>     <?php echo form_close(); ?> </div><!--<div class="reg_form">-->     </div><!--<div id="content">--> 

and module :-

class user_model extends ci_model {      public function __construct()     {         parent::__construct();     }     function login($email,$password)     {         $this->db->where("email",$email);         $this->db->where("password",$password);          $query=$this->db->get("user");         if($query->num_rows()>0)         {             foreach($query->result() $rows)             {                 //add data session                 $newdata = array(                         'user_id'       => $rows->id,                         'user_name'     => $rows->username,                         'user_email'    => $rows->email,                         'logged_in'     => true,                    );             }                 $this->session->set_userdata($newdata);                 return true;                     }         return false;     }     public function add_user()     {         $data=array(             'username'=>$this->input->post('user_name'),             'email'=>$this->input->post('email_address'),             'password'=>md5($this->input->post('password'))             );         $this->db->insert('user',$data);     } } 

change:

$this->form_validation->set_rules('user_name', 'user name', 'trim|required|min_length[4]|xss_clean'); $this->form_validation->set_rules('email_address', 'your email', 'trim|required|valid_email'); 

to

$this->form_validation->set_rules('user_name', 'user name', 'trim|required|min_length[4]|is_unique[users.user_name]');  $this->form_validation->set_rules('email_address', 'your email', 'trim|required|min_length[4]|valid_email|is_unique[users.email_address]'); 

if form_validation.php not have is_unique() function, add:

public function is_unique($str, $field) {     list($table, $field) = explode('.', $field);      if (isset($this->ci->db))     {         $query = $this->ci->db->limit(1)->get_where($table, array($field => $str));         return $query->num_rows() === 0;     }      return false; } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -