<?php /** * Test Controller. * * @package app * @extends Controller_Rest */ class Controller_Test extends Controller_Rest { public function before() { parent::before(); $this->session = \Fuel\Core\Session::instance(); } public function action_test() { $this->session->set('user_id', 12345); $this->response(array('sessionId' => $this->session->key('session_id'))); } }The first time I call /test/test I receive
{"sessionId":false}
but the new session cookie is correctly sent in the response headers. The second time I request /test/test the cookie is sent with the request headers and I then receive the expected response
{"sessionId":"9dddf3fcb058c181bee70e0238581880"}
<?php /** * Test Controller. * * @package app * @extends Controller_Rest */ class Controller_Test extends Controller_Rest { public function before() { parent::before(); $this->session = \Fuel\Core\Session::instance(); if (!$this->session->key('session_id')) { $this->session->write(); } else { $this->session->rotate(false); } } public function action_test() { $this->session->set('user_id', 12345); $this->response(array('sessionId' => $this->session->key('session_id'))); } }
ErrorException [ Notice ]: Undefined index: created
-namespace Fuel\Core;
-
// --------------------------------------------------------------------
class Session_Db extends \Session_Driver
@@ -79,10 +75,10 @@
public function read($force = false)
{
// get the session cookie
- $cookie = $this->_get_cookie();
+ $session_id = (Input::post($this->config['post_cookie_name'])) ?: false;
// if no session cookie was present, initialize a new session
- if ($cookie === false or $force)
+ if ($session_id === false or $force)
{
$this->data = array();
$this->keys = array();
@@ -90,31 +86,19 @@
else
{
// read the session record
- $this->record = \DB::select()->where('session_id', '=', $this->keys['session_id'])->from($this->config['table'])->execute($this->config['database']);
+ $this->record = \DB::select()->where('session_id', '=', $session_id)->from($this->config['table'])->execute($this->config['database']);
// record found?
if ($this->record->count())
{
+ $this->_set_keys();
$payload = $this->_unserialize($this->record->get('payload'));
}
else
{
- // try to find the session on previous id
- $this->record = \DB::select()->where('previous_id', '=', $this->keys['session_id'])->from($this->config['table'])->execute($this->config['database']);
-
- // record found?
- if ($this->record->count())
- {
- // previous id used, correctly set session id so it wont be overwritten with previous id.
- $this->keys['session_id'] = $this->record->get('session_id');
- $payload = $this->_unserialize($this->record->get('payload'));
- }
- else
- {
// cookie present, but session record missing. force creation of a new session
return $this->read(true);
}
- }
if (isset($payload[0])) $this->data = $payload[0];
if (isset($payload[1])) $this->flash = $payload[1];
@@ -138,9 +122,6 @@
{
parent::write();
- // rotate the session id if needed
- $this->rotate(false);
-
// create the session record, and add the session payload
$session = $this->keys;
$session['payload'] = $this->_serialize(array($this->data, $this->flash));
@@ -158,13 +139,8 @@
}
// update went well?
- if ($result !== false)
+ if ($result === false)
{
- // then update the cookie
- $this->_set_cookie();
- }
- else
- {
logger(\Fuel::L_ERROR, 'Session update failed, session record could not be found. Concurrency issue?');
}
@@ -273,4 +249,24 @@
return parent::_validate_config($validated);
}
+ /**
+ * assign values to the keys that would otherwise have been
+ * set when decrypting the cookie
+ *
+ * @access private
+ * @return void
+ */
+ protected function _set_keys()
+ {
+ if ($this->record->count())
+ {
+ $this->keys['session_id'] = $this->record->get('session_id');
+ $this->keys['previous_id'] = $this->record->get('session_id');
+ $this->keys['ip_hash'] = $this->record->get('ip_hash');
+ $this->keys['user_agent'] = $this->record->get('user_agent');
+ $this->keys['created'] = $this->record->get('created');
+ $this->keys['updated'] = $this->time->get_timestamp();
}
+ }
+
+}
Thank you It looks like you're new here. If you want to get involved, click one of these buttons!