CodeIgniter Login with Session

Installing CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter which is 3.1.7 that I've used in this source code. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as codeigniter_login so I'm using the below code.
  1. localhost/codeigniter_login/
codeigniter successful install

Creating our Database

First we are going to create our database and insert sample data for this tutorial.
  1. Open your phpMyAdmin.
  2. Create a new database named codeigniter.
  3. Click the database that we created, click SQL tab then paste the below code or import the included .sql file in the downloadable of this source code located in db folder.
  1. CREATE TABLE `users` (
  2. `email` varchar(40) NOT NULL,
  3. `password` varchar(40) NOT NULL,
  4. `fname` varchar(100) NOT NULL,
  1. INSERT INTO `users` (`id`, `email`, `password`, `fname`) VALUES
  2. (1, '[email protected]', 'nurhodelta', 'Neovic Devierte'),
  3. (2, '[email protected]', 'cepe', 'Gemalyn Cepe');
database mysql You can then use the ff. login credentials: Email: [email protected] Password: nurhodelta Email: [email protected] Password: cepe

Connecting our App into our Database

Next, we're going to connect our codeigniter application to the database that we created earlier.
  1. In your codeigniter app folder, open database.php located in application/config folder.
  2. Update database.php with your credential the same as what I did in the image below.

database is where we define the database that we created earlier.

database config codeigniter

Configuring Base URL

Next, we configure our base url to tell codeigniter that this is the URL of our site/application. We're gonna be using this a lot that's why we need to configure this. 1. In your codeigniter app folder, open config.php located in application/config folder. 2. Find and edit the ff line:
  1. $config['base_url'] = 'http://localhost/codeigniter_login';
where codeigniter_login is the name of your app folder.

Creating our Models

Next, we create the model for our app. Take note that the first letter of your model name should be in CAPITAL letter and the name of the model should be the same as the file name to avoid confusion. Create a file named Users_model.php in application/models folder of our app and put the ff codes.
  1. <?php
  2. class Users_model extends CI_Model {
  3. function __construct(){
  4. parent::__construct();
  5. $this->load->database();
  6. }
  7.  
  8. public function login($email, $password){
  9. $query = $this->db->get_where('users', array('email'=>$email, 'password'=>$password));
  10. return $query->row_array();
  11. }
  12.  
  13. }
  14. ?>

Creating our Controller

Next step is to create our controller. Controllers follow the same naming convention as models. Create a file named User.php in application/controllers folder of our app and put the ff codes.
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class User extends CI_Controller {
  5.  
  6. function __construct(){
  7. parent::__construct();
  8. $this->load->helper('url');
  9. $this->load->model('users_model');
  10. }
  11.  
  12. public function index(){
  13. //load session library
  14. $this->load->library('session');
  15.  
  16. //restrict users to go back to login if session has been set
  17. if($this->session->userdata('user')){
  18. redirect('home');
  19. }
  20. else{
  21. $this->load->view('login_page');
  22. }
  23. }
  24.  
  25. public function login(){
  26. //load session library
  27. $this->load->library('session');
  28.  
  29. $email = $_POST['email'];
  30. $password = $_POST['password'];
  31.  
  32. $data = $this->users_model->login($email, $password);
  33.  
  34. if($data){
  35. $this->session->set_userdata('user', $data);
  36. redirect('home');
  37. }
  38. else{
  39. header('location:'.base_url().$this->index());
  40. $this->session->set_flashdata('error','Invalid login. User not found');
  41. }
  42. }
  43.  
  44. public function home(){
  45. //load session library
  46. $this->load->library('session');
  47.  
  48. //restrict users to go to home if not logged in
  49. if($this->session->userdata('user')){
  50. $this->load->view('home');
  51. }
  52. else{
  53. redirect('/');
  54. }
  55.  
  56. }
  57.  
  58. public function logout(){
  59. //load session library
  60. $this->load->library('session');
  61. $this->session->unset_userdata('user');
  62. redirect('/');
  63. }
  64.  
  65. }

Creating our Routes

Next, we are going to set our default route and add a new route for our home which is the goto page after a successful login. Open routes.php located in application/config folder and set the default route to our user controller. Note: While we name controllers using CAPITAL letter in this first letter, we refer to them in SMALL letter.
  1. $route['default_controller'] = 'user';
  2. $route['home'] = 'user/home';

Creating our Views

Lastly, we create the views of our app. Take note that I've use Bootstrap in the views. You may download bootstrap using this link. Create the ff files inside application/views folder. login_page.php This contains our login page.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeIgniter Login</title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">CodeIgniter Login with Session</h1>
  11. <div class="row">
  12. <div class="col-sm-4 col-sm-offset-4">
  13. <div class="login-panel panel panel-primary">
  14. <div class="panel-heading">
  15. <h3 class="panel-title"><span class="glyphicon glyphicon-lock"></span> Login
  16. </h3>
  17. </div>
  18. <div class="panel-body">
  19. <form method="POST" action="<?php echo base_url(); ?>index.php/user/login">
  20. <fieldset>
  21. <div class="form-group">
  22. <input class="form-control" placeholder="Email" type="email" name="email" required>
  23. </div>
  24. <div class="form-group">
  25. <input class="form-control" placeholder="Password" type="password" name="password" required>
  26. </div>
  27. <button type="submit" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  28. </fieldset>
  29. </form>
  30. </div>
  31. </div>
  32. <?php
  33. if($this->session->flashdata('error')){
  34. ?>
  35. <div class="alert alert-danger text-center" style="margin-top:20px;">
  36. <?php echo $this->session->flashdata('error'); ?>
  37. </div>
  38. <?php
  39. }
  40. ?>
  41. </div>
  42. </div>
  43. </div>
  44. </body>
  45. </html>
home.php This is our login success landing page.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeIgniter Login</title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">CodeIgniter Login with Session</h1>
  11. <div class="row">
  12. <div class="col-md-4 col-md-offset-4">
  13. <?php
  14. $user = $this->session->userdata('user');
  15. extract($user);
  16. ?>
  17. <h2>Welcome to Homepage </h2>
  18. <h4>User Info:</h4>
  19. <p>Fullname: <?php echo $fname; ?></p>
  20. <p>Email: <?php echo $email; ?></p>
  21. <p>Password: <?php echo $password; ?></p>
  22. <a href="<?php echo base_url(); ?>index.php/user/logout" class="btn btn-danger">Logout</a>
  23. </div>
  24. </div>
  25. </div>
  26. </body>
  27. </html>

Demo

That ends the explanation for this tutorial. Happy Coding :)

Comments

Submitted byDonald X (not verified)on Fri, 04/16/2021 - 19:51

Codeigniter 4.11 error File: login_page.php:
  1. <?php
  2. if($this->session->flashdata('error')){
  3. ?>
Message: Undefined property: CodeIgniter\View\View::$session What is the problem?
Submitted byhissein yakhoub (not verified)on Mon, 12/04/2023 - 19:45

Ce très bien

Add new comment