public function action_login() { $this->template->title = 'login'; $this->template->content = View::factory('welcome/index'); $data = null; if (Input::method() == 'POST') { if (!Security::check_token()) { Session::set_flash('error', 'the form has expired please refresh the page and try again'); return false; } $val = Validation::factory('login'); $val->add('email')->add_rule('valid_email')->add_rule('required')->add_rule('min_length', 3); $val->add('password')->add_rule('required')->add_rule('min_length', 8); if (Input::method() == 'POST' && $val->run()) { $password = Input::post('password'); $email = Input::post('email'); $user = Model_User::find() ->related('admin') ->where('email', $email) ->get_one(); if (!$user) { Session::set_flash('error', 'invalid email / password combo'); return false; } $session = Session::create(); if ($user->login($password) == true) { $session->set('user_id', $user->id); $session->set('authenticated', true); if ($user->timezone) { $session->set('timezone', $user->timezone); } if ($user->admin) { $session->set('admin', true); } Response::redirect('home'); return; } } else { $data['errors'] = $val->errors(); Session::set_flash('error', 'invalid email / password combo'); return false; } } }
class Controller_Session extends Controller { public $template = 'maintemplate'; public $auto_render = true; public $user_id; public $admin = false; // Load the template and create the $this->template object public function before($data = null) { $session = Session::instance(); if ($session->get('authenticated') == false || $session->get('user_id') == null) { Response::redirect('welcome/index'); return true; } $this->user_id = $session->get('user_id'); if ($session->get('admin') == true) { $this->admin = true; } }
public function action_logout() { $session = Session::instance(); $session->set('authenticated', false); $session->set('admin', null); $session->set('user_id', null); Response::redirect('welcome/index'); return true; }
// reset the stored session data $this->keys = $this->flash = $this->data = array();The static methods all use "Session::instance()", the default session. Which should be the same as calling a dynamic method on the default instance. If you are using static calls everywhere, why not use Session::destroy() in your logout method? And check if you have auto_initialize on in your config. If so, you don't need to create a session, it will be created automatically when you load the Session class. In this case, a manual create just creates a new session (which for example in case of the db driver, doubles the number of session records!).
public function action_logout() { Session::delete('authenticated'); Session::delete('admin'); Session::delete('user_id'); Response::redirect('welcome/index'); return true; }
Brian Perin wrote on Friday 21st of October 2011:I changed all session getters/setters to static calls and its the same. This is probably a better methodpublic function action_logout() { Session::delete('authenticated'); Session::delete('admin'); Session::delete('user_id'); Response::redirect('welcome/index'); return true; }
But still if I throw in Session::destroy() call , those variables seem to come back some how and I'll get bounced back to the session controller like the user is still logged in. I don't really get whats going on. The above works fine, I just don't understand why Session::destroy() isn't destroying the variables.
Session::set()will use the default session instance, whereas if you use
$session = Session::instance('blah');it will grab the 'blah' session instance, which can be completely different. Is there a particular reason you aren't using the Auth package? It's extremely flexible.
It looks like you're new here. If you want to get involved, click one of these buttons!