Skip to content

Commit

Permalink
Issue #21: Session handler checking for memcached uses memcache funct…
Browse files Browse the repository at this point in the history
…ions

This now uses memcached functions to determine if the session handler is
valid. Additionally it adds support for Moodle PHP7 error handling.
  • Loading branch information
nhoobin committed Jun 20, 2017
1 parent fa97887 commit 290a705
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,33 @@ function failed($reason) {
failed('sitedata not readable');
}

$sessionhandler = (property_exists($CFG, 'session_handler_class') && $CFG->session_handler_class === '\core\session\memcached');
$savepath = property_exists($CFG, 'session_memcached_save_path');

$sessionhandler = (property_exists($CFG, 'session_handler_class') && $CFG->session_handler_class == '\core\session\memcached');
if ($sessionhandler && $savepath) {
require_once($CFG->libdir . '/classes/session/util.php');
$servers = \core\session\util::connection_string_to_memcache_servers($CFG->session_memcached_save_path);
try {
$memcached = new \Memcached();
$memcached->addServers($servers);
$stats = $memcached->getStats();
$memcached->quit();

if ($sessionhandler) {
$addr = $servers[0][0];
$port = $servers[0][1];

if ($stats[$addr . ':' . $port]['uptime'] > 0) {
$status .= "session memcached OK<br>\n";
} else {
failed('sessions memcached');
}

$memcache = explode(':', $CFG->session_memcached_save_path );
try {
memcache_connect($memcache[0], $memcache[1], 3);
$status .= "session memcache OK<br>\n";
} catch (Exception $e) {
failed('sessions memcache');
failed('sessions memcached');
} catch (Throwable $e) {
failed('sessions memcached');
}

}

// Optionally check database configuration and access (slower).
Expand All @@ -137,6 +152,8 @@ function failed($reason) {
}
} catch (Exception $e) {
failed('database error');
} catch (Throwable $e) {
failed('database error');
}
}

Expand Down

0 comments on commit 290a705

Please sign in to comment.