BASE . LOG,
'log_file_id' => $LOG_FILE_ID,
'log_per_date' => true,
]);
use CoreLibs\Create\Session;
$PAGE_NAME = 'TEST CLASS: SESSION';
print "";
print "
" . $PAGE_NAME . "";
print "";
print '';
print '' . $PAGE_NAME . '
';
$session_name = 'class-test-session';
print "Valid session name static check for '" . $session_name . "': "
. \CoreLibs\Debug\Support::prBl(Session::checkValidSessionName($session_name)) . "
";
$var = 'foo';
$value = 'bar';
$session = new Session($session_name);
foreach (['123', '123-123', '123abc'] as $_session_name) {
print "[UNSET] Session Name valid for '" . $_session_name . "': "
. ($session->checkValidSessionName($_session_name) ? 'Valid' : 'Invalid') . "
";
}
echo "Global session name: " . ($GLOBALS['SET_SESSION_NAME'] ?? '-') . "
";
print "[SET] Current session id: " . $session->getSessionId() . "
";
print "[SET] Current session name: " . $session->getSessionName() . "
";
print "[SET] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "
";
print "[SET] Current session auto write close: " . ($session->checkAutoWriteClose() ? 'Yes' : 'No') . "
";
print "[SET] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "
";
if (isset($_SESSION)) {
print "[SET] _SESSION is: set
";
} else {
print "[SET] _SESSION is: not set
";
}
#
if (!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 0;
}
$_SESSION['counter']++;
print "[READ] A " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "
";
$_SESSION[$var] = $value;
print "[READ] B " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "
";
print "[READ] Confirm " . $var . " is " . $value . ": "
. (($_SESSION[$var] ?? '') == $value ? 'Matching' : 'Not matching') . "
";
// test set wrappers methods
$session->set('setwrap', 'YES, method set _SESSION var');
print "[READ WRAP] A setwrap: " . $session->get('setwrap') . "
";
print "[READ WRAP] Isset: " . ($session->isset('setwrap') ? 'Yes' : 'No') . "
";
$session->unset('setwrap');
print "[READ WRAP] unset setwrap: " . $session->get('setwrap') . "
";
print "[READ WRAP] unset Isset: " . ($session->isset('setwrap') ? 'Yes' : 'No') . "
";
// test __get/__set
$session->setwrap = 'YES, magic set _SESSION var'; /** @phpstan-ignore-line GET/SETTER */
print "[READ MAGIC] A setwrap: " . ($session->setwrap ?? '') . "
";
print "[READ MAGIC] Isset: " . (isset($session->setwrap) ? 'Yes' : 'No') . "
";
unset($session->setwrap);
print "[READ MAGIC] unset setwrap: " . ($session->setwrap ?? '') . "
";
print "[READ MAGIC] unset Isset: " . (isset($session->setwrap) ? 'Yes' : 'No') . "
";
print "
";
// differnt session name
$session_name = 'class-test-session-ALT';
try {
$session_alt = new Session($session_name);
print "[3 SET] Current session id: " . $session_alt->getSessionId() . "
";
print "[SET AGAIN] Current session id: " . $session_alt->getSessionId() . "
";
} catch (\Exception $e) {
print "[3 FAILED] Session start failed:
" . $e->getMessage() . "
" . $e . "
";
}
print "[ALL SESSION]: " . \CoreLibs\Debug\Support::printAr($_SESSION) . "
";
// close session
$session->writeClose();
// will never be written
$_SESSION['will_never_be_written'] = 'empty';
// auto open session if closed to write
$session->set('auto_write_session', 'Some value');
// restart session
$session->restartSession();
$_SESSION['this_will_be_written'] = 'not empty';
// open again with same name
$session_name = 'class-test-session';
try {
$session_alt = new Session($session_name, auto_write_close:true);
print "[4 SET] Current session id: " . $session_alt->getSessionId() . "
";
print "[4 SET] Current session auto write close: " . ($session_alt->checkAutoWriteClose() ? 'Yes' : 'No') . "
";
print "[START AGAIN] Current session id: " . $session_alt->getSessionId() . "
";
$session_alt->set('alt_write_auto_close', 'set auto');
// below is deprecated
// $session_alt->do_not_do_this = 'foo bar auto set';
} catch (\Exception $e) {
print "[4 FAILED] Session start failed:
" . $e->getMessage() . "
" . $e . "
";
}
$_SESSION['will_be_written_again'] = 'Full';
print "[ALL SESSION]: " . \CoreLibs\Debug\Support::printAr($_SESSION) . "
";
// close session
$session->writeClose();
// invalid
$session_name = '123';
try {
$session_bad = new Session($session_name);
print "[5 SET] Current session id: " . $session_bad->getSessionId() . "
";
} catch (\Exception $e) {
print "[5 FAILED] Session start failed:
" . $e->getMessage() . "
" . $e . "
";
}
print "";
// __END__