Loading...

codeigniter tutorial

CodeIgniter Example

in this example based on session ,if the user clicked on back button , it will not open login page once user loges in,and also it will check all functions if session is set or not if not it will navigate to loginpage

database.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
	'dsn'	=> 'pgsql:host=localhost;dbname=kwikl',
	'hostname' => 'localhost',
	'username' => 'postme',
	'password' => 'hi1234',
	'database' => 'kwik_l3arn',
	'dbdriver' => 'pdo',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

 config.php

the below code will check wheather the project is running in live or local

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$ServerPath = $_SERVER['DOCUMENT_ROOT'];
$scriptname = explode('/', $_SERVER['SCRIPT_NAME']);
if ((isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS'])) !== 'off') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) {
    $WebPath = 'https://';
} else {
    $WebPath = 'http://';
}
$WebPath .= isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
$BaseURL = $WebPath; //. '/' . $scriptname[1];
$EBASEURL = $WebPath; //. '/' . $scriptname[1];
for ($i = 1; $i < count($scriptname) - 1; $i++) {
    $BaseURL .= '/' . $scriptname[$i];
}
$config['base_url'] = $BaseURL;

autoload.php

$autoload['libraries'] = array('database', 'email', 'session');
$autoload['helper'] = array('url', 'file','form');
$autoload['model'] = array('Demo_Model');

Demo_Controller.php

class Demo_Controller extends CI_Controller {

    public function __construct() {

        parent::__construct();
        //this will check for each function weather user is log or not
        if($this->uri->segment(1) != "" && $this->uri->segment(2) != "lck" )
        {
            if(empty($this->session->userdata['sessiondata'])) {
                redirect(base_url());
            }
        }
        // $this->output->enable_profiler(TRUE);

    }

    public function index() {
        //if the user is already login it will go to home page
        if (isset($this->session->userdata['sessiondata'])) {
            $this->lck();
            exit;
        } else {
        //if not login it will show login page
            $this->login();
        }
    }

    public function login() {
        $this->Demo_Model->login();
        $this->output->delete_cache();
        $this->load->view('login');
    }

   //this will check if the user is login or not
    public function lck() {
        if (isset($this->session->userdata['sessiondata'])) {
            redirect('Welcome');
        } else {
            $id = $this->input->post("uname");
            $pass = $this->input->post("upass");
            $verify = $this->Demo_Model->userchk($id, $pass);
            if ($verify == 1) {
                $result = $this->session->userdata['sessiondata'];

                redirect('Welcome');
            } else {
                $this->session->set_flashdata('usercheck', 'Invalid Username or Password');
                $this->load->view('login');
            }
        }
    }

     public function hme() {
       
        $this->load->view('home');
    }

     //logout function destroy all session
     public function lout() {

        if (isset($this->session->userdata['sessiondata'])) {
            $this->session->sess_destroy();
            redirect(base_url());

            exit;
        } else {
            $this->lck();
            exit;
        }
    }

}

Demo_Model.php

<?php
class Demo_Model extends CI_Model {
    public function __construct() {
        parent::__construct();
        error_reporting(0);
    }
     //user login check
    public function userchk($id, $pass) {
        $this->db->select("*");
        $this->db->from("users");
        $this->db->where("user_id", $id);
        $this->db->where("user_password", $pass);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            $session_data = $query->row_array();
            $this->session->set_userdata('sessiondata', $session_data);
                    return 1;
        } else {
            return 0;
        }
    }
    
    }

routes.php

$route['default_controller'] = 'Demo_Controller/index';
$route['Welcome']='Demo_Controller/hme';

login.php

<!DOCTYPE HTML>
<html>
<body>
 
    <div class="w3ls-pos">
      
        <div class="w3ls-login box">
            <!-- form starts here -->
            <form action="<?php echo base_url(); ?>Demo_Controller/lck" method="post">
                <div class="agile-field-txt">
                    <input type="text" name="uname" id="uname" placeholder="user name" required="" />
                </div>
                <div class="agile-field-txt">
                    <input type="password" name="upass" id="upass" placeholder="******" required="" id="myInput" />

                </div>
                <span style="color:red ;font-family: fantasy"><?php echo $this->session->flashdata('usercheck');  ?></span>
                <div class="w3ls-bot">
                    <input type="submit" value="LOGIN">
                </div>
            </form>
        </div>
 
    </div>
    <!-- scripts required  for particle effect -->
    <script src='<?php echo base_url(); ?>Assets/web/js/particles.min.js'></script>
    <script src="<?php echo base_url(); ?>Assets/web/js/index.js"></script>
    <!-- //scripts required for particle effect -->
</body>

</html> 

home.php

<div class="login"> <a href="#"><i class="fa fa-user" aria-hidden="true"></i><span>
                                            <?php $result = $this->session->userdata['sessiondata'];
                                            echo $result['user_name']; ?></span></a>
</div>
<div class="login"> <a href="<?php echo base_url() ?>Demo_Controller/lout"><i class="fa fa-power-off" aria-hidden="true"></i> <span>Logout</span></a>
</div>