0

I can't save my custom session on database with codeigniter and i don't know why.

This is my config.php

$config['encryption_key'] = 'MY_KEY';

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 0;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

And in my Model i have this:

foreach ($res as $user) {
    $usuario["name"] = $user->name;
    $usuario["lastname"] = $user->lastname;
    $usuario["date_birthday"] = $user->date_birthday;
    $usuario["date_register"] = $user->date_register;
    $usuario["last_season"] = $user->last_seasson;
    $usuario["email"] = $user->email;
    $usuario["contry"] = $user->contry;
    $usuario["sex"] = $user->sex;
    $usuario["role"] = $user->role;
    $usuario["status"] = $user->status;
    $usuario["idUsuario"] = $user->id;
}

$this->set_last_season($usuario["idUsuario"]);

$this->session->set_userdata('usuario', $usuario);

And in my Controller, I try view my session with:

var_dump($this->session->userdata('usuario'));

And I created my session table in database with:

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`)
);

All this code works fine without saving session on database and using cookie sessions

Please, i need help.... i don't know that try more... Thanks for all

2
  • The code does look right, but I'd first make sure the $usario is being set. var_dump($usario) before trying to add it to the session. Perhaps your foreach isn't populating it properly.
    – sitesbyjoe
    Oct 1, 2014 at 21:24
  • Yeah, thanks for your answer. I have checked the array $usuario and contains the expected data. The login Controller, I've been stopping the flow of the application, and I keep the session into the controller, but when you leave that controller, either by a redirect ("/", "refresh") or manually by URL lose the session.
    – Vzk91
    Oct 2, 2014 at 6:20

2 Answers 2

0

Well I followed exhaustively analyzing the flow of my application, and I detected the fault came in the redirect from the login controller to any other controller. At that time, was losing the sessiones codeigniter.

I was documenting this failure, and saw that there were very many more people affected and did not believe that this ruling embers unresolved. So here after searching thoroughly in Google, I found an interesting article that was describing this problem and provided a "patch" for the Sessions library files included in codeigniter system.

This library I did was to reuse all the methods of the original library but applying it to a use of native sessiones. Thus the passage of sessiones between controllers did not fail.

What if someone else has had the same problem, or for future developers who have left the link in the article which is the code of the new library session.

http://www.philsbury.co.uk/blog/code-igniter-sessions

0

Change:

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

for:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.