Welcome Guest, Not a member yet? Register   Sign In
Session Login/Logout script help
#1

[eluser]mikeymayhem[/eluser]
Hi
I am having trouble trying to get my login/logout script to work. I am fairly new to codeigniter and have been working on a small user based blog as a learning project. Basically i have a controller called users which manages all the registration, login and logout processes. When the users logs in successfully the script reirects the user to an account controller which will display recent activites and other profile related material. How ever at the moment the script isnt working as i intend as it still allows users to view the account page if they arent logged in. Ive tried everyway i can think of to get this working and im sure its something really simple that i am missing but i am at my wits end Big Grin PLEASE HELP code below!
#2

[eluser]mikeymayhem[/eluser]
Code:
function login() {
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="errorform">', '</div>');
        
        $this->form_validation->set_rules('username_login','Username','trim|required|xss_clean');
        $this->form_validation->set_rules('password_login','Password','trim|required|xss_clean|md5');

        if($this->form_validation->run() == FALSE)
        {
        //if the form validation throws back errors reload the login
        //view and display the errors
            $data['main_content'] = 'users/login_view';
            $this->load->view('includes/template',$data);
            //redirect('users/register');
        }
        else
        {
        //prep the form data and check against users database
            $username_login = $this->input->post('username_login');
            $password_login = $this->input->post('password_login');

            $result = $this->users_model->login($username_login, $password_login);
            if($result->num_rows() > 0){
                $user_details = $result->row();
                $data = array(
                'username'  =>  $username_login,
                'is_logged_in' => TRUE,
                'user_id'   => $user_details->id,
                'user_type' => $user_details->user_type  
                );
                $this->session->set_userdata($data);
                redirect('account');
            }
            else
            {
                $this->session->set_flashdata('message_login','Sorry your username or password could not be found, Please try again');
                redirect('users/login');
            }
        }
    }
    function logout(){
        
        $this->session->sess_destroy();
        $this->session->unset_userdata('is_logged_in');
        redirect('users/login');
    }
#3

[eluser]mikeymayhem[/eluser]
Code:
&lt;?php
class Account extends Controller {

    function Account()
    {
        parent::Controller();
        $this->load->model('account_model');
    }
    function index() {
            $is_logged_in = $this->session->userdata('is_logged_in');
            if(!isset($is_logged_in) AND $is_logged_in != TRUE){
                //$this->session->set_flashdata('message_login','sorry you must be logged in to view that page');
                $data['main_content'] = 'users/login';
                $this->load->view('includes/template');
            }else{
                $user_id = $this->session->userdata('user_id');
                $data['latest_comments'] = $this->account_model->getlatest_comments($user_id);

                $data['main_content'] = 'users/account_view';
                $this->load->view('includes/template',$data);
            }
    }
}
#4

[eluser]mikeymayhem[/eluser]
Hi i have fixed the problem thanks to this thread http://ellislab.com/forums/viewthread/94981/ basically sess_destroy was destroying the session but not clear the variables that were set in the log in process the function now looks like this after adding
this->session->sess_create();
Code:
function logout(){
        
        $this->session->sess_destroy();
        $this->session->sess_create();
        $this->session->set_flashdata('message_login','You have successfully logged out');
        redirect('users/login');
    }




Theme © iAndrew 2016 - Forum software by © MyBB