Hi , I m new to codeigniter and i m trying to build a login system. However the logout system is not working well. After I click logout in /main/home it shows URL error. anyone know whats the problem ?

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

class main extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        $this->load->helper('url');
        $this->load->helper('form');
        session_start();
    }
    public function index()
    {
        $this->login();


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

    }


    public function denied()
    {
        $this->load->view('denied');
    }
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('main/login');
    }


    public function login_validation()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'required|min_length[3]|max_length[12]');
        $this->form_validation->set_rules('password', 'Password', 'required|sha1|callback_password_check');

        if ($this->form_validation->run()){
            $data = array(
            'password' => $this->input ->post('password '),
            'logged' => 1
            );
            $this->session->set_userdata($data);
            redirect('main/home');
            } else {

            $this->load->view('login');
            }
    }       

    public function password_check()
    {

        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->load->model('users');

        if($this->users->log_in($username, $password)){
            return True;
            }else{
            $this->form_validation->set_message('password_check','The username or password is not correct.');
            return False;
    }
    }

    public function checklogged()
    {

    if ($this->session->userdata('logged'))
    {
        }else{
        redirect('main/denied');




}



    }
    }

I don't think session_start() is needed in the constructor.

If you do this after logging out

print_r($this->session->all_userdata());

do you still get something ?

You can try modifying this code

 'password' => $this->input ->post('password '),
 'logged' => 1

to this

 'password' => $this->input ->post('password'),
 'logged' => TRUE

another question I have, why do you have to store password in session. Is the member id number is not enough?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.