vendredi 19 août 2016

Destroy session codeigniter

i used session in my code. when i tried to logout it's working. but when i klick back, the program still can reach the admin page again. i don't know why. i think it has been destroyed. when the admin page was refresh, session works. the program back in to homepage. the point is i have to refresh first to make my session work out.

this is my code.

the controller for login :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller{
        function __construct()
    {
        parent::__construct();
        $this->load->model('model_user'); //memasukkan file model m_login.php ke dalam controller
    }
    function index()
    {
        $session = $this->session->userdata('isLogin'); //mengabil dari session apakah sudah login atau belum
        if($session == FALSE) //jika session false maka akan menampilkan halaman login
        {
            $this->load->view('home/index_home');
        }else //jika session true maka di redirect ke halaman dashboard
        {
            redirect('dashboard/index');
        }
    }
        
    function do_login()
    {
        $username = $this->input->post("uname");
        $password = $this->input->post("pass");
        
        $cek = $this->model_user->cek_user($username,md5($password)); //melakukan persamaan data dengan database
        if(count($cek) == 1){ //cek data berdasarkan username & pass
            foreach ($cek as $cek) {
                $level = $cek['level']; //mengambil data(level/hak akses) dari database
            }
            $this->session->set_userdata(array(
                'isLogin'   => TRUE, //set data telah login
                'uname'         => $username, //set session username
                'lvl'           => $level, //set session hak akses
            ));
                
            redirect('dashboard/index','refresh');  //redirect ke halaman dashboard
        }else{ //jika data tidak ada yng sama dengan database
            echo "<script>alert('Gagal Login!')</script>";
            redirect('home','refresh');
        }
        
    }

}

the controller for dashboard :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
        public function __construct(){
                parent::__construct();
                $this->auth->cek_auth(); 
                
        }
        
        public function ceklogin(){
                $session = $this->session->userdata('isLogin');
        if($session == FALSE)
        {
                $this->load->view('home/index_home');
        }
        }
        
        public function index()
        {
                $this->ceklogin();
                
                $stat = $this->session->userdata('lvl');
                
                if($stat=='admin'){
                        $this->load->view('admin/home/index_admin',$data); 
                }else{ 
                        $this->load->view('member/home/index_member',$data);
                }
                
        }

public function logout()
        {
                $this->session->sess_destroy();
                
                redirect('home','refresh');
        }
}

The auth in the folder libraries:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth {
    public function cek_auth()
        {
                $this->ci =& get_instance();
                $this->sesi  = $this->ci->session->userdata('isLogin');
                $this->hak = $this->ci->session->userdata('stat');
                if($this->sesi != TRUE){
                        redirect('home','refresh');
                        exit();
                }
                
        }
        public function hak_akses($kecuali="")
        {       
        if($this->hak==$kecuali){ 
                echo "<script>alert('Anda tidak berhak mengakses halaman ini!');</script>";
                redirect('dashboard/index');
        }elseif ($this->hak=="") {
                echo "<script>alert('Anda belum login!');</script>";
                redirect('home');
        }else{

        }
        }
}

Aucun commentaire:

Enregistrer un commentaire