codeigniter
Unable to retrieve session value in CI (CodeIgniter) either in localhost or server or switching controller?

Unable to retrieve session value in CI (CodeIgniter) either in localhost or server or switching controller?

Are you unable to retrieve session value in CodeIgniter either in localhost or while switching controller. If so, this article is for you.

Here are the scenarios you might be getting issue.

  • Trying to get session either from server or in localhost.
  • You are able to get session value within same controller but once you try to get the stored session from model or from other controllers you won’t be able to get it.

If you are getting issue as mentioned above, won’t worry, you will be able to fix it. But before that, did you search for this issue. Once you search you will get following fixes. So, before my answer, try to attempt these steps.

Store your data in session

$data = array(
  'user_id' => $row['user_id'],
  'username' => $row['username']
);
$this->session->set_userdata($data);

Get session data

$userID = $this->session->userdata('user_id');
$username = $this->session->userdata('username');

Don’t forget to add session library in autoload

application/config/autoload.php

$autoload['libraries'] = array('session');

OR

load in controller __contruct() function

$this->load->library('session');

Did above steps work? Nope? No program. Let’s try new style which is really awesome. Following these steps

  • First goto CodeIgniter official page to download latest version
  • Now in your existing project rename the folder system to old_system. Just incase you don’t miss your important files created in system folder.
  • Now copy system folder of latest CodeIgniter folder.
  • Paste that system folder to your existing project.

Now run your project. You might get issues. If so, just copy newly created files and folder in old_system folder to new system folder.

And that’s all. Isn’t that too easy? I hope so. Well, using this approach I was able to fix my problem. And I hope this fixed yours as well. If not, just let me know and I will try to help you.

If this helped you, don’t forget to share this post so that others might get help. That would be highly appreciated.

Thank you

?Peace?

Leave a Reply