/** * Mark session as accessed, prevents timeouts. * * @deprecated since 2.6 * @param string $sid */ function session_touch($sid) { debugging('session_touch() is deprecated, use \\core\\session\\manager::touch_session() instead', DEBUG_DEVELOPER); \core\session\manager::touch_session($sid); }
public function test_touch_session() { global $DB; $this->resetAfterTest(); $sid = md5('hokus'); $record = new \stdClass(); $record->state = 0; $record->sid = $sid; $record->sessdata = null; $record->userid = 2; $record->timecreated = time() - 60 * 60; $record->timemodified = time() - 30; $record->firstip = $record->lastip = '10.0.0.1'; $record->id = $DB->insert_record('sessions', $record); $now = time(); \core\session\manager::touch_session($sid); $updated = $DB->get_field('sessions', 'timemodified', array('id' => $record->id)); $this->assertGreaterThanOrEqual($now, $updated); $this->assertLessThanOrEqual(time(), $updated); }
/** * Receives an array of usernames from a remote machine and prods their * sessions to keep them alive * * @param array $array An array of usernames * @return string "All ok" or an error message */ function keepalive_server($array) { global $CFG, $DB; $remoteclient = get_mnet_remote_client(); // We don't want to output anything to the client machine $start = ob_start(); // We'll get session records in batches of 30 $superArray = array_chunk($array, 30); $returnString = ''; foreach ($superArray as $subArray) { $subArray = array_values($subArray); $instring = "('" . implode("', '", $subArray) . "')"; $query = "select id, session_id, username from {mnet_session} where username in {$instring}"; $results = $DB->get_records_sql($query); if ($results == false) { // We seem to have a username that breaks our query: // TODO: Handle this error appropriately $returnString .= "We failed to refresh the session for the following usernames: \n" . implode("\n", $subArray) . "\n\n"; } else { foreach ($results as $emigrant) { \core\session\manager::touch_session($emigrant->session_id); } } } $end = ob_end_clean(); if (empty($returnString)) { return array('code' => 0, 'message' => 'All ok', 'last log id' => $remoteclient->last_log_id); } return array('code' => 1, 'message' => $returnString, 'last log id' => $remoteclient->last_log_id); }
// // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. /** * Ensure that session is kept alive. * * @copyright 2014 Andrew Nicols * @package core * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ define('AJAX_SCRIPT', true); require_once __DIR__ . '/../config.php'; // Require the session key - want to make sure that this isn't called // maliciously to keep a session alive longer than intended. if (!confirm_sesskey()) { header('HTTP/1.1 403 Forbidden'); print_error('invalidsesskey'); } // Update the session. \core\session\manager::touch_session(session_id());