Пример #1
0
function generateRandomKey($length = 64)
{
    // Get the characters a session key can consist of
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+=[]{}\\|/?<>,.`~';
    // Generate a random session key
    $randomKey = '';
    for ($i = 0; $i < $length; $i++) {
        $randomKey .= $chars[rand(0, strlen($chars) - 1)];
    }
    // Return the random key
    return $randomKey;
}
// Get the session cookie name
$sessionCookieName = Registry::getValue('client.session.cookie.name')->getValue();
// Make sure the session cookie is set
if (!CookieManager::hasCookie($sessionCookieName)) {
    // Get the session timeout
    $sessionTimeout = Registry::getValue('client.session.timeout')->getValue();
    // Generate a session key
    $sessionKey = generateRandomKey();
    // Set the session
    CookieManager::setCookie($sessionCookieName, $sessionKey, $sessionTimeout);
    // Set the session key global
    $GLOBALS['session_key'] = $sessionKey;
}
/**
 * Get the key of the current client session.
 *
 * @return string Session key
 *
 * @throws Exception Throws if an error occurred.