Blog

Check a Submitted Email Against a Database Table to Ensure it hasn’t Already Been Added using CodeIgniter with Model, Controller, and View Code Examples
Posted on July 2, 2015 in CodeIgniter, MVC, MySQL, PHP by Matt Jennings

Model

<?php
class UserDashboardModel extends CI_Model
{

    // Check login email against
    // users table in database to
    // ensure it doesn't already exist
    public function check_login_email($submitted_email, $submitted_password)
    {
        return $this->db->query("SELECT * FROM users WHERE (email = ? AND password = ?)", array($submitted_email, $submitted_password))->row_array();
    }

}
?>

Controller

<?php
class UserDashboard extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();

        // Load the UserDashboardModel on all pages
        $this->load->model('UserDashboardModel');

        $this->output->enable_profiler();
    }

    // Error validation and redirect to:
    // admin page OR
    // normal user page
    public function checksignin()
    {

        $sec_email = $this->input->post('email', TRUE);
        $password = $this->input->post('password', TRUE);
        $sec_password = substr(md5($password), 0, -2);

        $check_user = $this->UserDashboardModel->check_login_email($sec_email, $sec_password);

        // If user exists in database...
        if($check_user)
        {
            // Set user first/last name as variable
            $hello_user_name = 'Hello, ' . $check_user['first_name'] . ' ' . $check_user['last_name'];

            // If user_level is "admin"
            // go to page and create an "admin_session" variable AND
            // create a session variable with a first AND
            // last name
            if($check_user['user_level'] == 'admin')
            {
                $this->session->set_userdata('admin_session', 'admin_session');

                $this->session->set_userdata('admin_user_name', $hello_user_name);

                redirect(base_url() . 'dashboard/admin');
            }
            // Elseif user_level is "normal"
            // go to page and create an "normal_session" variable AND
            // create a session variable with a first AND
            // last name
            elseif($check_user['user_level'] == 'normal')
            {
                $this->session->set_userdata('normal_session', 'normal_session');

                $this->session->set_userdata('normal_user_name', $hello_user_name);

                redirect(base_url() . 'dashboard');
            }

        }
        else
        {
            // If the user doesn't exist in the database
            // redirect to the sign in page and
            // display the error session variable below
            $this->session->set_userdata('not_in_db_error', '<strong><p>Your email/password are invalid. Please try again.</p></strong>');

            redirect(base_url() . 'signin');
        }
    }

}
?>

View

<?php
// Display user error if it exists
echo $this->session->userdata('not_in_db_error');
$this->session->unset_userdata('not_in_db_error');
?>

<form id="signin-register-add" action="<?php echo base_url() . 'dashboard/checksignin'; ?>" method="post">
    <input type="hidden" name="checksignin"/>

    <div class="row-fluid">
        <div class="col-md-3"><p><label for="email">Email Address:</label></p></div>
        <div class="col-md-9"><p><input type="text" name="email"/></p></div>
    </div>

    <div class="row-fluid">
        <div class="col-md-3"><p><label for="email">Password:</label></p></div>
        <div class="col-md-9"><p><input type="password" name="password"/></p></div>
    </div>

    <div class="row-fluid">
        <div class="col-md-9 col-md-offset-3"><p><input type="submit" value="Sign In"/></p></div>
    </div>
</form>

Leave a Reply