How to logout with public function

42 views Asked by At

im using the code below to logout and i dont know how to logout with a public function, how to logout with button modal and using function, this is my function from login.php

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

class Login extends CI_Controller
{

    //this function will do the login process
    function __construct()
    {
        parent::__construct();
        $this->load->library('session');
    }


    function index()
    {
        $this->load->library('session');
        if ($this->session->username != NULL) {
            redirect('index.php/report/dashboard_ch');
        } else {
            $this->load->view('login');
        }
    }



    function do_login()
    {
        $this->load->library('session');
        $this->load->model('M_login');
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $dataLogin = false;

        if ($username === "dimasdev") {
            $dataLogin = $this->M_login->web_login_hashed($username, $password);
        } else {
            $dataLogin = $this->M_login->web_login($username, $password);
        }

        if (!$dataLogin) {
            $res['msg'] = "Invalid username or password";
            $res['status'] = false;
            echo json_encode($res);
            exit();
        }

        $this->session->set_userdata($dataLogin);

        $res['msg'] = "Login Success";
        $res['dataLoginUsername'] = $dataLogin['username'];
        $res['status'] = true;
        echo json_encode($res);
    }

    public function logout()
    {
        $this->session->sess_destroy(); // Destroy the session
        $res['msg'] = "Logout Success";
        $res['status'] = true;
        echo json_encode($res);
    }
}

this is my logout modal from my navbar, im using button to logout, and i dont know how to logout

<div class="modal fade" id="logoutModal" tabindex="-1" aria-labelledby="logoutModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="logoutModalLabel">Are you sure you want to log out?</h5>
            </div>
            <div class="modal-footer">
                <div class="col s2" style="float: center">
                    <button class="btn btn-danger" href="<?php echo base_url(); ?>index.php/login/logout" class="col s6 modal-close waves-effect waves-green btn-flat yes">OK</button>
                </div>
                <div class="col s2" style="float: center">
                    <button class="btn btn-secondary" data-bs-dismiss="modal" class="col s6 modal-close waves-effect waves-green btn-flat no">Cancel</button>
                </div>
            </div>
        </div>
    </div>
</div>

i build this code with vuexy html framework can you help ?

1

There are 1 answers

0
Alexander Kurz On

Just a little HTML error.

<a href="URL">LABEL</a>

button dont have href attribute, if you want to you use a button just use it as LABEL

<a href="URL"><button></button></a>

in youre case

<div class="col s2" style="float: center">
    <a href="<?php echo base_url() . "index.php/login/logout"; ?>">
        <button class="btn btn-danger" class="col s6 modal-close waves-effect waves-green btn-flat yes">OK</button>
    </a>
</div>

maybe additional '/'

href="<?php echo base_url() . "/index.php/login/logout"; ?>"

not sure