diff options
author | Florian Pritz <bluewind@xinu.at> | 2012-08-28 13:56:37 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2012-08-28 15:12:34 +0200 |
commit | 25c2eb78d91ee581916f90dd7b248b86a72c5f2b (patch) | |
tree | 616430d595d0b488364e0a9dad83c91d6e92e4aa /application | |
parent | 9f73bdbc9c19570768f24e1fbb672a59afbf5f1b (diff) |
Only create new sessions when the are really needed
Most sessions are just people viewing a paste. Those don't need a
session until they want to log in so we don't have to pollute the
database and waste resources.
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application')
-rw-r--r-- | application/controllers/user.php | 1 | ||||
-rw-r--r-- | application/models/muser.php | 41 |
2 files changed, 39 insertions, 3 deletions
diff --git a/application/controllers/user.php b/application/controllers/user.php index add6e8bdc..ef79baa78 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -36,6 +36,7 @@ class User extends CI_Controller { function login() { + $this->muser->require_session(); $this->session->keep_flashdata("uri"); if ($this->input->post('process')) { diff --git a/application/models/muser.php b/application/models/muser.php index 8ec4de4c5..c29c04df6 100644 --- a/application/models/muser.php +++ b/application/models/muser.php @@ -4,18 +4,51 @@ class Muser extends CI_Model { function __construct() { parent::__construct(); - $this->load->library("session"); + + if ($this->has_session()) { + $this->session->keep_flashdata("uri"); + } + $this->load->helper("filebin"); - $this->session->keep_flashdata("uri"); + } + + function has_session() + { + // checking $this doesn't work + $CI =& get_instance(); + if (property_exists($CI, "session")) { + return true; + } + + // Only load the session class if we already have a cookie that might need to be renewed. + // Otherwise we just create lots of stale sessions. + if (isset($_COOKIE[$this->config->item("sess_cookie_name")])) { + $this->load->library("session"); + return true; + } + + return false; + } + + function require_session() + { + if (!$this->has_session()) { + $this->load->library("session"); + } } function logged_in() { - return $this->session->userdata('logged_in') == true; + if ($this->has_session()) { + return $this->session->userdata('logged_in') == true; + } + + return false; } function login($username, $password) { + $this->require_session(); $query = $this->db->query(' SELECT username, id, password FROM `users` @@ -42,6 +75,7 @@ class Muser extends CI_Model { function logout() { + $this->require_session(); $this->session->unset_userdata('logged_in'); $this->session->unset_userdata('username'); $this->session->sess_destroy(); @@ -74,6 +108,7 @@ class Muser extends CI_Model { echo "FileBin requires you to have an account, please go to the homepage for more information.\n"; exit(); } else { + $this->require_session(); if (!$this->session->userdata("flash:new:uri")) { $this->session->set_flashdata("uri", $this->uri->uri_string()); } |