Registration of new user in CodeIgniter 3 PHP Framework
In this Part, we will see the Registration of new users.
For Registration of new users, We need a Registration page where users can enter details and get registered.
Registration page UI looks.
Before going ahead we need to add a link on the login page where whenever the user clicks, it will redirect to the Registration page.
Create a link for registration as below on the login page.
On click this link it will call the "registration" function of the "users" controller.
So we need to define registration function in users controller as below.
Update Code in the controller file: CRUDProject\application\controllers\Users.php
In the registration function, we have added code-Igniter form level validations.
- For email, we are validating if the entered email is a valid email and at the same time it's validating with email_check() function.
email_check() will check if entered mail is already exist in the database.
- For password and confirm password we are checking with the password if it matches.
Please check the below-highlighted code.
In the above-highlighted code, we are calling a function getRows($email) defined in the User model, which will fire a query to check if any data is present with the same email.
As it will call function present in the User model, we need to add code for insert values.
Update Code in the model file: CRUDProject\application\models\User.php
After this you need to add registration.php file inside views directory:
Create a file : CRUDProject\application\views\users\registration.php
Now you need to run this code and check all the functions.
Youtube video link for your reference.
For Registration of new users, We need a Registration page where users can enter details and get registered.
Registration page UI looks.
Before going ahead we need to add a link on the login page where whenever the user clicks, it will redirect to the Registration page.
Create a link for registration as below on the login page.
<p>Don't have an account? <a href="<?php echo base_url('index.php/users/registration'); ?>">Register</a></p> |
On click this link it will call the "registration" function of the "users" controller.
So we need to define registration function in users controller as below.
Update Code in the controller file: CRUDProject\application\controllers\Users.php
In the registration function, we have added code-Igniter form level validations.
- For email, we are validating if the entered email is a valid email and at the same time it's validating with email_check() function.
email_check() will check if entered mail is already exist in the database.
- For password and confirm password we are checking with the password if it matches.
Please check the below-highlighted code.
//User Registration Code public function registration(){ $data = $userData = array(); // If registration request is submitted if($this->input->post('signupSubmit')){ $this->form_validation->set_rules('first_name', 'First Name', 'required'); $this->form_validation->set_rules('last_name', 'Last Name', 'required'); $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check'); $this->form_validation->set_rules('password', 'password', 'required'); $this->form_validation->set_rules('conf_password', 'confirm password', 'required|matches[password]'); $userData = array( 'first_name' => strip_tags($this->input->post('first_name')), 'last_name' => strip_tags($this->input->post('last_name')), 'email' => strip_tags($this->input->post('email')), 'password' => md5($this->input->post('password')), 'gender' => $this->input->post('gender'), 'phone' => strip_tags($this->input->post('phone')) ); if($this->form_validation->run() == true){ $insert = $this->user->insert($userData); if($insert){ $this->session->set_userdata('success_msg', 'Your account registration has been successful. Please login to your account.'); redirect('users/login'); }else{ $data['error_msg'] = 'Some problems occured, please try again.'; } }else{ $data['error_msg'] = 'Please fill all the mandatory fields.'; } } $data['user'] = $userData; $this->load->view('users/registration', $data); } //To check if email is already exist in database public function email_check($str){ $checkEmail = $this->user->getRows($str); if($checkEmail > 0){ $this->form_validation->set_message('email_check', 'The given email already exists.'); return FALSE; }else{ return TRUE; } } |
In the above-highlighted code, we are calling a function getRows($email) defined in the User model, which will fire a query to check if any data is present with the same email.
As it will call function present in the User model, we need to add code for insert values.
Update Code in the model file: CRUDProject\application\models\User.php
//insert user details public function insert($data = array()) { if(!empty($data)){ if(!array_key_exists("created", $data)){ $data['created'] = date("Y-m-d H:i:s"); } $insert = $this->db->insert($this->table, $data); return $insert?$this->db->insert_id():false; } return false; } public function getRows($email){ $this->db->where('email',$email); return $user = $this->db->get('users')->row_array(); } |
After this you need to add registration.php file inside views directory:
Create a file : CRUDProject\application\views\users\registration.php
<?php include('header.php')?> <div class="sidenav"> <div class="login-main-text"> <h2><b>Application<br> Registration Page</b></h2> <p>Login or register from here to access.</p> </div> </div> <div class="main"> <div class="col-md-6 col-sm-12"> <div class="register-form"> <!-- Status message --> <?php if(!empty($success_msg)){ echo '<div class="alert alert-dismissible alert-success"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>'.$success_msg.'</strong></div>'; }elseif(!empty($error_msg)){ echo '<div class="alert alert-dismissible alert-danger"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>'.$error_msg.'</strong></div>'; } ?> <!-- Registration form --> <form action="" method="post"> <div class="form-group"> <label>First Name</label> <input type="text" class="form-control" name="first_name" placeholder="FIRST NAME" value="<?php echo !empty($user['first_name'])?$user['first_name']:''; ?>" required> <?php echo form_error('first_name','<p class="help-block">','</p>'); ?> </div> <div class="form-group"> <label>Last Name</label> <input type="text" class="form-control" name="last_name" placeholder="LAST NAME" value="<?php echo !empty($user['last_name'])?$user['last_name']:''; ?>" required> <?php echo form_error('last_name','<p class="help-block">','</p>'); ?> </div> <div class="form-group"> <label>User Email</label> <input type="email" class="form-control" name="email" placeholder="EMAIL" value="<?php echo !empty($user['email'])?$user['email']:''; ?>" required> <?php echo form_error('email','<p class="help-block">','</p>'); ?> </div> <div class="form-group"> <label>Password</label> <input type="password" class="form-control" name="password" placeholder="PASSWORD" required> <?php echo form_error('password','<p class="help-block">','</p>'); ?> </div> <div class="form-group"> <label>Confirm Password</label> <input type="password" class="form-control" name="conf_password" placeholder="CONFIRM PASSWORD" required> <?php echo form_error('conf_password','<p class="help-block">','</p>'); ?> </div> <div class="form-group"> <label>Gender: </label> <?php if(!empty($user['gender']) && $user['gender'] == 'Female'){ $fcheck = 'checked="checked"'; $mcheck = ''; }else{ $mcheck = 'checked="checked"'; $fcheck = ''; } ?> <div class="radio"> <label> <input type="radio" name="gender" value="Male" <?php echo $mcheck; ?>>Male</label> <label> <input type="radio" name="gender" value="Female" <?php echo $fcheck; ?>>Female</label> </div> </div> <div class="form-group"> <label>Phone Number</label> <input type="text" class="form-control" name="phone" placeholder="PHONE NUMBER" value="<?php echo !empty($user['phone'])?$user['phone']:''; ?>"> <?php echo form_error('phone','<p class="help-block">','</p>'); ?> </div> <div class="send-button"> <input type="submit" name="signupSubmit" class="btn btn-primary" value="CREATE ACCOUNT"> </div> </form> <br/> <p>Already have an account? <a href="<?php echo base_url('index.php/users/login'); ?>">Login here</a></p> </div> </div> </div> <?php include('footer.php')?> |
Now you need to run this code and check all the functions.
Youtube video link for your reference.
Comments
Post a Comment