From 25c2eb78d91ee581916f90dd7b248b86a72c5f2b Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 28 Aug 2012 13:56:37 +0200 Subject: 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 --- application/controllers/user.php | 1 + 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()); } -- cgit v1.2.3-24-g4f1b