Codeigniter User Registration and Login Script - Mostlikers

25 July, 2016

Codeigniter User Registration and Login Script

Today we are going to learn about how to create simple user registration, login script with the session. Here I have created a simple users register,login, and logout module coding. For form validation,email validation,session validations i have used CodeIgniter validation libraries functions.

Codeigniter User Registration and Login Script

Live demo                  Download

Database

Create a new 'user_registration' table using the below code. 
CREATE TABLE IF NOT EXISTS `user_registration` (
  `id` int(11) NOT NULL,
  `name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(250) NOT NULL
)


Flow steps for user registration system.

  • Create a simple register and login HTML form layout. Load HTML page to your main or access controller.
  • To validating the form use CodeIgniter validation libraries. must you have to  check unique email in the database.
  • After register, the user sends a new email to verify they email id. 
  • Check your controller whether session values time to exit or not.

Controller(users.php)

Create a new controller object name called (users) load the below function to that controller.
<?php
class users extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->model('users_model','users'); 
        // users is users_model alias name        
    }

    public function index()
    {
        $this->load->view('signin_form');   
    }

    public function login()
    {
        $this->form_validation->set_rules('email', 'email id ', 'trim|required');
        $this->form_validation->set_rules('password', 'password', 'trim|required');
        if ($this->form_validation->run() == FALSE):
            $this->load->view('signin_form');             
        else :
            $email     = $this->input->post('email');
            $password  = md5($this->input->post('password'));
            $log_users = $this->users->user_login($email,$password);
            if(is_object($log_users)) :
               $session_data=array('user_name'=>$log_users->name,
               'session_id'=>$log_users->id); 
               $this->session->set_userdata('user_session_info', $session_data);
               redirect(site_url('users/profile'));  
            else :
                $this->session->set_flashdata('error', 'Invalid username or password.');
                $this->load->view('signin_form');
            endif;            
        endif;
    }

    public function signup()
    {
        $this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('new_password', 'Password', 'trim|required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation',
            'trim|required|matches[new_password]');
        $this->form_validation->set_rules('user_email', 'Email', 
            'required|is_unique[user_registration.email]');
        $this->form_validation->set_message('is_unique', 'The %s is already taken');

        if ($this->form_validation->run() == FALSE):
            $this->load->view('signin_form');             
        else :
            $data = array(
                'name'=>$this->input->post('username'),
                'email'=>$this->input->post('user_email'),
                'password'=>md5($this->input->post('new_password')),
            );
            $this->db->insert('user_registration',$data);
            unset($_POST);
            $this->session->set_flashdata('message', 'Successfully register.');
            redirect(site_url('users'));
        endif;
    }

    public function profile()
    {
        if($this->session->userdata['user_session_info']) :
            $session_info = $this->session->userdata['user_session_info'];
            $this->data['user_information'] = $this->users->get_user(
            $session_info['session_id']);
            $this->load->view('profile',$this->data);            
        else :
            $this->session->set_flashdata('error', 'Please login before access this page.');
            redirect(site_url('users'));
        endif;
    }

    public function logout()
    {
        $this->session->unset_userdata('user_session_info');
        redirect(site_url('users'));       

    }

}


  • $this->load->model('users_model','users') - load the users model users is a alias name of usersmodel
  • $this->session->set_userdata('user_session_info', $session_data) - user_session_info it's a session name we are storing user information to that session.
  • $this->session->set_flashdata - it will be store temporary session to that page.
  • $this->form_validation->set_rules -   Validation rules either form value true or false checking conditions.

Models (users_model.php)

<?php
class Users_model extends CI_Model
{
    public function user_login($email,$password)
    {
        $sql = "SELECT * FROM user_registration 
                WHERE email='$email' AND password='$password' LIMIT 1 ";
        $result = $this->db->query($sql)->row();        
        return $result; 
    }

    public function get_user($id)
    {
        $sql = "SELECT * FROM user_registration WHERE id='$id' LIMIT 1 ";
        $result = $this->db->query($sql)->row();        
        return $result; 
    }  
}


View(signin_form.php)

<h3>Login form </h3>
<form method="post" action="<?php echo site_url('users/login') ?>">
  <h5>Email</h5>
  <input type="text" name="email" value="<?php echo set_value('email'); ?>" />
  <div class="errorMessage"><?php echo form_error('email'); ?></div>
  <h5>Password</h5>
  <input type="password" name="password" value="<?php echo set_value('password'); ?>" />
  <div class="errorMessage"><?php echo form_error('password'); ?></div>
  <input type="submit" class="btn btn-primary" value="Submit" />
</form>

<h3>New user regsirtation</h3>
<form method="post" action="<?php echo site_url('users/signup') ?>">
<h5>Name</h5>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" />
<div class="errorMessage"><?php echo form_error('username'); ?></div>
<h5>Email Address</h5>
<input type="text"  name="user_email" value="<?php echo set_value('user_email'); ?>" />
<div class="errorMessage"><?php echo form_error('user_email'); ?></div>
<h5>Password</h5>
<input type="password" name="new_password" value="<?php echo set_value('new_password'); ?>"
 />
<div class="errorMessage"><?php echo form_error('new_password'); ?></div>
<h5>Password Confirm</h5>
<input type="password" name="passconf" value="<?php echo set_value('passconf'); ?>" />
<div class="errorMessage"><?php echo form_error('passconf'); ?></div>
<input type="submit" class="btn btn-primary" value="Submit" />
</form>


Session flashes message

<!-- Success Message -->
<?php if($this->session->flashdata('message')){?>
    <div class="success"> 
      <?php echo $this->session->flashdata('message')?>
    </div>
<?php } ?>
<!-- /Success Message -->
<!-- Error Message -->
<?php if($this->session->flashdata('error')){?>
  <div class="error">
    <?php echo $this->session->flashdata('error')?> 
  </div>
<?php } ?>
<!-- Error Message -->


profile.php

<h3><b> Welcome to profile page  </b></h3><br>
<p>Name : <?=$user_information->name; ?></p>
<p>Email : <?=$user_information->email; ?></p>
<p>Click here to <a href="<?=site_url('users/logout')?>">logout</a></p>








Related Topics


2 comments:

  1. Boleto, Ek Dum Zakkas ....

    ReplyDelete
  2. I know I can treat you better... ��

    ReplyDelete