Welcome Guest, Not a member yet? Register   Sign In
Writing session data seemingly inconsistently
#1

Experimenting with a new project using 3.0.0 under IIS 7 and PHP 5.3.8.

This is a fresh Igniter directory, not an upgrade.

I seem to be getting inconsistent results when writing session data through the new Sessions library.

This is basically user log-in model code migrated over from an older CIv1.73 project.  (The controller & model code was migrated, not the CI installation.)

The Session library initializes according to the log, using the files driver.

When initially accessing the page, my site controller sets an anonymous user ID to the session data.  I see the session data as being set.  __ci_last_regenerate and an anonymous userID are set.  It seems obvious that the session data is at least partially setting.

Upon login, my controller calls a model function that checks the posted credentials and when matched, sets the username and userID session data and returns a logged-in flag to the controller.  Log traces show the code being called - or, at least, the log trace is the line following the set_userdata calls.  On successful login, the controller does a header redirect to the site landing page.  Log traces show that redirect being done.

On loading the default controller, I then - as in any load - check the userID, to see if it's anonymous or not, in the constructor.  At this time, it isn't seeing the logged-in userID, resets it as a new anonymous ID and redirects back to my login page.

I've modified the original code so that any time I do a header redirect, I call session_write_close() first to close out the session writes, but so far, I can't get the login working.

The basic code worked fine in CI 1.73.  I don't see why setting the session data from within a model function should prevent it from writing.
Reply
#2

Much has changed since 1.73. I know because that's about when I started using CI. I think if you read over all of the upgrade guides since 1.73 you will probably see something. That said, I just upgraded an app from 2.2.2 to 3.0.0 and found that the sessions class was very different. One cannot simply expect old session code to work, at least that is my experience.
Reply
#3

(07-06-2015, 07:32 PM)skunkbad Wrote: Much has changed since 1.73. I know because that's about when I started using CI. I think if you read over all of the upgrade guides since 1.73 you will probably see something. That said, I just upgraded an app from 2.2.2 to 3.0.0 and found that the sessions class was very different. One cannot simply expect old session code to work, at least that is my experience.

Oh, sure... I understand what you mean.

It's not so much expecting it to work (although honesty compels me to admit that I'd hoped it would), but being able to determine why it breaks and how to remedy it.

Obviously, I expect my issues to revolve about the fact that data is now stored in the native $_SESSION superglobal, instead of the serialized $_COOKIE referenced as a session.

It does seem odd that, so far, I seem to have been able to set a value from the controller level, but not from the model level.  It's more likely that I'm losing data between page redirects, rather than not being able to set it in the first place.
Reply
#4

For models you probably have to bring in the CI instance.

$CI =& get_instance();
$CI->session->set_userdata(array('key' => 'val'));
Reply
#5

(07-07-2015, 08:41 AM)CroNiX Wrote: For models you probably have to bring in the CI instance.

$CI =& get_instance();
$CI->session->set_userdata(array('key' => 'val'));

If a model extends the CI_Model (or a model which extends it), CI_Model's __get() magic method will call the CI instance for anything you reference via $this which is not found in the model itself.

Generally, you only need to assign the instance to a variable in libraries and helpers.
Reply
#6

I've run into situations where that is not true, albeit in CI 2.x. So thought I'd suggest it.
Reply
#7

(07-07-2015, 09:36 AM)CroNiX Wrote: I've run into situations where that is not true, albeit in CI 2.x. So thought I'd suggest it.

There's only one such situation - if you override CI_Model::__get().
Reply




Theme © iAndrew 2016 - Forum software by © MyBB