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

Monday, February 7, 2022

[FIXED] Restore session_id in CodeIgniter

 February 07, 2022     codeigniter, php, session     No comments   

Issue

I want to destroy restore the session id and then start the session, which is done in standard PHP like following. How can I do this in CodeIgniter?

session_destroy();
session_id($old_session_id);
session_start();

I think session is started automatically by autoloading the library:

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

Is it so? How can I use the session_id($old_session_id); in CodeIgniter? I'm trying with $this->session->set_userdata('session_id', $old_session_id);

But it seems that something is not working fine, as i can not get the session data. Any idea if anything is wrong with the above lines or something else? Thanks.


Solution

Some things to check for:

  1. Check to ensure sessions are working in PHP without CodeIgniter. If sessions do not work then your server is not saving session info correctly

  2. Check if you are saving session information to the database or not (In application/config/config.php does $config['sess_use_database'] equal FALSE? If TRUE, then make sure to have this code imported into your DB

 

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

If something is not correct here then CI is not saving session info.

  1. Try a bit more session testing using this snippet in a working controller function:

 

$this->load->library('session'); // load session in controller
echo $session_id = $this->session->userdata('session_id'); // print session id to page

Check your error logs to see if CI is loading the session library module and no errors are present.

This is more of a debugging strategy for sessions than a solution.



Answered By - jason_hamiltonm
  • 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