PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

[FIXED] Session is not getting unset when signing out

 March 01, 2022     codeigniter, codeigniter-2, php     No comments   

Issue

I have created admin panel in codeigniter. Sign in is working fine and data is also set in session. The issue is when I click on signout it doesn't clear the session data.

My controller signout function :

public function signout()
{
    
    $this->user_m->signout();
    redirect('admin','refresh');
}

user_m model signout function :

public function signout()
{
    $adminsignindata = array('loginname','adminusername','userid','role_id','loggedin');
    $this->session->unset_userdata($adminsignindata);
}

I don't know why session is not getting unset when I use array in unset_userdata.

In localhost it is working but in live not working. In live I had to unset userdata using key wise.


Solution

The CI 2.x session library contains the function unset_userdata(); which works either with a string or an associative array:

unset_userdata() can be used to remove it (the session), by passing the session key.

This function can also be passed an associative array of items to unset.

source: CI 2.x manual, Removing Session Data

below the function unset_userdata() in the session library, around line 500:

function unset_userdata($newdata = array())
{
    if (is_string($newdata))
    {
        $newdata = array($newdata => '');
    }

    if (count($newdata) > 0)
    {
        foreach ($newdata as $key => $val)
        {
            unset($this->userdata[$key]);
        }
    }

    $this->sess_write();
}

so in case, you continue with version 2.x you need to change $adminsignindata into an associative array:

$adminsignindata = array('loginname'=>'','adminusername'=>'','userid'=>'','role_id'=>'','loggedin'=>'');

in CI 3.x, this function was changed and accepts a "normal" array, as in your code example



Answered By - Vickel
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing