Codeigniter 2.1.3: sess_destroy() causes Undefined

2019-05-10 14:25发布

问题:

I have been testing out the codeigniter's feature with sessions using database, and when ever I log out (use sess_destroy()) i get the following notices:

            A PHP Error was encountered

            Severity: Notice

            Message: Undefined index: session_id

            Filename: libraries/Session.php

            Line Number: 272
            A PHP Error was encountered

            Severity: Notice

            Message: Undefined index: ip_address

            Filename: libraries/Session.php

            Line Number: 272
            A PHP Error was encountered

            Severity: Notice

            Message: Undefined index: user_agent

            Filename: libraries/Session.php

            Line Number: 272
            A PHP Error was encountered

            Severity: Notice

            Message: Undefined index: last_activity

            Filename: libraries/Session.php

            Line Number: 272
            A PHP Error was encountered

            Severity: Notice

            Message: Undefined index: session_id

            Filename: libraries/Session.php

            Line Number: 288
            A PHP Error was encountered

            Severity: Notice

            Message: Undefined index: last_activity

            Filename: libraries/Session.php

            Line Number: 289

What do I need to do to fix that? (I know that I can turn off error reporting ect, but I am more interested to why this happens and how to fix it).

I used this to create the table:

                CREATE TABLE IF NOT EXISTS  `ci_sessions` (
                        session_id varchar(40) DEFAULT '0' NOT NULL,
                        ip_address varchar(45) DEFAULT '0' NOT NULL,
                        user_agent varchar(120) NOT NULL,
                        last_activity int(10) unsigned DEFAULT 0 NOT NULL,
                        user_data text NOT NULL,
                        PRIMARY KEY (session_id),
                        KEY `last_activity_idx` (`last_activity`)
                );

The session lib is autoloaded.

回答1:

This is causing your problem, with the release of 2.1.3:

Fixed a bug (#1314) - Session Library method sess_destroy() didn't destroy the userdata array.

$this->tank_auth->logout() contains the function sess_destroy(); So after you have called this, you cannot use set_flashdata() in $this->_show_message().

What you need to do is create a new session. This is how your logout method should look like in auth.php:

/**
 * Logout user
 *
 * @return void
 */
function logout() {
    $this->tank_auth->logout(); // Destroys session
    $this->session->sess_create();
    $this->_show_message($this->lang->line('auth_message_logged_out'));
}


回答2:

Just to add a quick note to Ben's answer. He is correct in identifying the problem, but I found, because of the order of operations, it is more effective to add the create session line in the Tank_auth library.

Here is the revised logout() function in the Tank_auth.php library:

    /**
 * Logout user from the site
 *
 * @return  void
 */
function logout()
{
    $this->delete_autologin();

    // See http://codeigniter.com/forums/viewreply/662369/ as the reason for the next line
    $this->ci->session->set_userdata(array('user_id' => '', 'username' => '', 'status' => ''));

    $this->ci->session->sess_destroy();
    $this->ci->session->sess_create();
}


回答3:

Check your application/config/config.php file, and see if you have $config[‘sess_use_database’] = TRUE.



回答4:

I guess, refresh is what you are missing . Have you tried redirect('logout', 'refresh');



回答5:

For Zach: Since you are using Tank auth make sure your logout function is in this way:-

    function logout(){
        if ($this->tank_auth->is_logged_in()) {
            $this->tank_auth->logout();

            redirect('');           

        } 
    }

You can also autoload the most commonly used helpers and libraries :

$autoload['libraries'] = array('database','session','form_validation','pagination');
$autoload['helper'] = array('form','url','html');

Your config seems to be fine,may be you are trying to logout twice.If there is still issue please show your logout code.