60597

User activation registration with CodeIgniter

Question:

I'm trying to build a registration system with CodeIgniter. I have a controller called User with the 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 so good. If I go to the register page I get the registration form displayed. If I send the form and it passes the form validation checks, I get the success page, if the form has errors, I get the form back with some error messages.

Now what I want to do is the database stuff. I have some idea of how I can get the POST values from the registration form into my database, but no clue how I can check if a username or email already exists, and if so, display that error on the registration form. and when i register in form, how can i send activation to user email to active account in my site.

Here's my 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 and anyone can join</div> <?php echo validation_errors('<p class="error">'); ?> <?php echo form_open("user/registration"); ?>

<label for="user_name">User Name:</label> <input type="text" id="user_name" name="user_name" value="<?php echo set_value('user_name'); ?>" />

<label for="email_address">Your Email:</label> <input type="text" id="email_address" name="email_address" value="<?php echo set_value('email_address'); ?>" />

<label for="password">Password:</label> <input type="password" id="password" name="password" value="<?php echo set_value('password'); ?>" />

<label for="con_password">Confirm Password:</label> <input type="password" id="con_password" name="con_password" value="<?php echo set_value('con_password'); ?>" />

<input type="submit" class="greenButton" value="Submit" />

<?php echo form_close(); ?> </div><!--<div class="reg_form">--> </div><!--<div id="content">-->

and module is :-

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() as $rows) { //add all data to 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); } }

Answer1:

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 your Form_validation.php does not have is_unique() function, then 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; }

Recommend

  • unable to show file error message on the view page in codeigniter
  • Error on passing data from controller to view of Codeigniter
  • Django: Save Form Data to Database
  • Control code 0x6 causing XML error
  • Android Intent to open Email application from separate class?
  • How to preserve order of children to appear after their parents
  • Codeigniter: unable to display the uploaded image with spaces and capitalizing subsequent words
  • Parsing CSV to Array of Hashes
  • unable to upload multiple images to its upload folder
  • Saving multiple images for one product using one to many in codeigniter
  • Only show certain number of emails in imaplib
  • Inflate exception- error inflating textview
  • Codeigniter form validation multiple callbacks
  • form_validation class does not load (codeigniter)
  • APNs messages are delivered but not received on iOS device
  • C# “cannot assign field because it is a foreach iteration variable”
  • How can include component js after templete js
  • Excel 2007: Format of email address from Outlook 2007
  • Updating and removing unique join relationships in CakePHP
  • Negating Regex PO BOX
  • My first C# WPF ValidationRule is not firing
  • Separate ID and Class for JS and CSS
  • How to validate a year I enter in textbox using jquery rule?
  • Laravel at least one field is required
  • jquery validation - waiting for remote check to complete
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • Is there some graphical way to create my own configuration file on SonarLint?
  • 550 Access denied - Invalid HELO name
  • Validate child input components on submit with Vee-Validate and vue js 2
  • Login not working in Firefox in Meteor
  • Does Mobilefirst provide a provision to access web services directly?
  • htaccess add www if not subdomain, if subdomain remove www
  • How do I access an unhandled exception in an MVC Error view?
  • Transactional Create with Validation in ServiceStack Redis Client
  • Email verification using google app script and google forms
  • Email format validation in mvc3 view
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • sending/ receiving email in Java
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Does armcc optimizes non-volatile variables with -O0?