Skip to content

Instantly share code, notes, and snippets.

@Sentinel-7
Forked from andronex/delete_old_sessions.php
Created March 31, 2021 07:23
Show Gist options
  • Save Sentinel-7/a645a0e3409e3f7df6b366821f7fadc0 to your computer and use it in GitHub Desktop.
Save Sentinel-7/a645a0e3409e3f7df6b366821f7fadc0 to your computer and use it in GitHub Desktop.
Удаление старых сессий в таблице MODX посредством плагина, а не настройками PHP.
<?php
/**
на событие OnMODXInit
*/
$rand = rand(1, 1000);
if ($rand === 1) {
$gcMaxlifetime = (integer) $modx->getOption('session_gc_maxlifetime', null, @ini_get('session.gc_maxlifetime'), true);
$access = time() - $gcMaxlifetime;
$modx->exec("
DELETE FROM {$modx->getTableName('modSession')} WHERE `access` < {$access};
OPTIMIZE TABLE {$modx->getTableName('modSession')};
");
$modx->log(modX::LOG_LEVEL_ERROR, 'clearOldSessions: old sessions have been removed.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment