Search code examples
phpcodeignitersessioncodeigniter-3session-variables

Confusing the session codeigniter function, the page logout() function doesn't work


I have tried the codeigniter session in this article https://www.malasngoding.com/membuat-login-dengan-codeigniter/ with the following code

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

class User extends CI_Controller
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *        http://example.com/index.php/welcome
     *    - or -
     *        http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model('user_model');
        $this->load->library('session');
    }

    //Login sudah bisa
    function action_login()
    {
        $user_email = $this->input->post('user_email');
        $user_password = $this->input->post('user_password');
        $wheredatasession = array(
            'user_email' => $user_email,
            'user_password' => md5($user_password)
        );

        $cek = $this->user_model->ceklogintolong($wheredatasession)->num_rows();
        if ($cek > 0) {
            $data_session = array(
                'nama' => $user_email,
                'status' => "login"
            );
            $this->session->set_userdata($data_session);
//          $this->session->set_userdata($data_session);
//          echo "Berhasil";
//          print_r($where);
            redirect('User/homeinfouser');

        } else {
            echo "Pass uname salah";
//          print_r($where);
        }
    }

    //Login menuju home info sudah bisa http://localhost/webcismppgri/User/homeinfouser
    function homeinfouser()
    {
//      echo "OK Tolong";
//      $hasil['print'] = $this->user_model->getinfo();
        $hasil['print'] = $this->user_model->getinfo();
//      print_r($hasil);
//      $judul_user['juduldashboard'] = "Dashboard User";
        $this->load->view('templates/sbadmin/header');
//      $this->load->view('templates/dashboard/index',$judul_user);
//      $this->load->view('templates/dashboard/page _informasi', $judul_user);
        $this->load->view('templates/sbadmin/sidebar');
        $this->load->view('templates/dashboard/page_informasi', $hasil);
//      $this->load->view('templates/sbadmin/footer');
        $this->load->view('templates/sbadmin/footer');
//      var_dump($hasilview);
//      $this->load->view('templates/dashboard/indextesdata',$hasil);
    }

    //Fungsi Logout
    function logout()
    {
        $this->session->sess_destroy();
        redirect('Landing', 'refresh');
    }



}

Tried to click the navigation back to the chrome browser page (right arrow navigation or forward), it can still be opened with the previously accessed page, even though I have provided the following code

// Logout function
function logout ()
{
$ this-> session-> sess_destroy ();
redirect ('Landing', 'refresh');
}

Solution

  • The correct one looks like this..

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class User extends CI_Controller
    {
    
        /**
         * Index Page for this controller.
         *
         * Maps to the following URL
         *        http://example.com/index.php/welcome
         *    - or -
         *        http://example.com/index.php/welcome/index
         *    - or -
         * Since this controller is set as the default controller in
         * config/routes.php, it's displayed at http://example.com/
         *
         * So any other public methods not prefixed with an underscore will
         * map to /index.php/welcome/<method_name>
         * @see https://codeigniter.com/user_guide/general/urls.html
         */
        public function __construct()
        {
            parent::__construct();
            $this->load->helper(array('form', 'url'));
            $this->load->model('user_model');
            $this->load->library('session');
            if (!$this->session->userdata('status')) {
                redirect('Landing','refresh');
            }
        }
    
        //Login sudah bisa
        function action_login()
        {
            $user_email = $this->input->post('user_email');
            $user_password = $this->input->post('user_password');
            $wheredatasession = array(
                'user_email' => $user_email,
                'user_password' => md5($user_password)
            );
    
            $cek = $this->user_model->ceklogintolong($wheredatasession)->num_rows();
            if ($cek > 0) {
                $data_session = array(
                    'nama' => $user_email,
                    'status' => "login"
                );
                $this->session->set_userdata($data_session);
    //          $this->session->set_userdata($data_session);
    //          echo "Berhasil";
    //          print_r($where);
                redirect('User/homeinfouser');
    
            } else {
                echo "Pass uname salah";
    //          print_r($where);
            }
        }
    
        //Login menuju home info sudah bisa http://localhost/webcismppgri/User/homeinfouser
        function homeinfouser()
        {
    //      echo "OK Tolong";
    //      $hasil['print'] = $this->user_model->getinfo();
            $hasil['print'] = $this->user_model->getinfo();
    //      print_r($hasil);
    //      $judul_user['juduldashboard'] = "Dashboard User";
            $this->load->view('templates/sbadmin/header');
    //      $this->load->view('templates/dashboard/index',$judul_user);
    //      $this->load->view('templates/dashboard/page _informasi', $judul_user);
            $this->load->view('templates/sbadmin/sidebar');
            $this->load->view('templates/dashboard/page_informasi', $hasil);
    //      $this->load->view('templates/sbadmin/footer');
            $this->load->view('templates/sbadmin/footer');
    //      var_dump($hasilview);
    //      $this->load->view('templates/dashboard/indextesdata',$hasil);
        }
    
        //Fungsi Logout
        function logout()
        {
            $this->session->sess_destroy();
            redirect('Landing', 'refresh');
        }
    
    
    
    }
    

    it worked according to the scenario, thank you for participating.