Index: no_anon.module =================================================================== --- no_anon.module (revision 5862) +++ no_anon.module (working copy) @@ -1,17 +1,18 @@ uid == 0) { + if (!session_save_session() || ($user->uid == 0 && empty($_SESSION))) { + // no_anon: We delete any existing session cookie because the session is empty for this user + session_delete_cookie(); return TRUE; } - db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, isset($user->cache) ? $user->cache : '', ip_address(), $value, time(), $key); + // If we made it this far, then we have session data worth saving. Create the cookie if user doesn't have one. + if (!isset($_COOKIE[session_name()])) { + // read what params the session cookie would've had if we hadn't disabled its creation + $cookie_params = session_get_cookie_params(); + // then create it ourselves + setcookie(session_name(), session_id(), time()+$cookie_params['lifetime'], $cookie_params['path'], $cookie_params['domain'], $cookie_params['secure']); + } + + + // Now update the sessions table to actually save the data + db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, isset($user->cache) ? $user->cache : '', $_SERVER['REMOTE_ADDR'], $value, time(), $key); if (db_affected_rows()) { // Last access time is updated no more frequently than once every 180 seconds. // This reduces contention in the users table. @@ -81,7 +94,7 @@ else { // If this query fails, another parallel request probably got here first. // In that case, any session data generated in this request is discarded. - @db_query("INSERT INTO {sessions} (sid, uid, cache, hostname, session, timestamp) VALUES ('%s', %d, %d, '%s', '%s', %d)", $key, $user->uid, isset($user->cache) ? $user->cache : '', ip_address(), $value, time()); + @db_query("INSERT INTO {sessions} (sid, uid, cache, hostname, session, timestamp) VALUES ('%s', %d, %d, '%s', '%s', %d)", $key, $user->uid, isset($user->cache) ? $user->cache : '', $_SERVER['REMOTE_ADDR'], $value, time()); } return TRUE; @@ -99,7 +112,7 @@ // regardless of the Drupal configuration. // TODO: remove this when we require at least PHP 4.4.0 if (isset($_COOKIE[session_name()])) { - setcookie(session_name(), '', time() - 42000, '/'); + session_delete_cookie(); } session_regenerate_id(); @@ -132,6 +145,12 @@ */ function sess_destroy_sid($sid) { db_query("DELETE FROM {sessions} WHERE sid = '%s'", $sid); + + // no_anon: make sure to remove unneeded cookie on logout. + // First make sure the session we're deleting is the current one. + if($sid = session_id()) { + session_delete_cookie(); + } } /** @@ -174,3 +193,19 @@ } return ($save_session); } + +/** + * Deletes session cookie for current user, if one exists + * + * Works by looking at the parameters of the current cookie and recreating it with + * expiration in the past. Note that bad things can happen if you call session_delete_cookie() + * and then try to set the cookie later in the same request. + */ +function session_delete_cookie() { + if (isset($_COOKIE[session_name()])) { + // get params of current cookie + $cookie_params = session_get_cookie_params(); + // and then re-set it with expiration time in the past + setcookie(session_name(), '', time() - 42000, $cookie_params['path'], $cookie_params['domain'], $cookie_params['secure']); + } +}