Skip to content
Youmy001 edited this page Feb 4, 2018 · 2 revisions

Sessions are the link allowing to transmit information about the user accross many requests in a web based service. They basically allow to maintain a state for the user in the application over a stateless protocol.

Login Verification

The basic feature of the session module is to store wheiter if a user is logged in for the current session. To access the session manager, you must import the SessionManager class from the Session module.

use Apine\Session\SessionManager;

To verify if a user is logged in, use the method is_logged_in() of the SessionManagerclass.

if (SessionManager::is_logged_in()) {
    // A user is logged in
}

The login() method allows to register a 'logged in' state for the provided user in the current session. The methods requires the name of the user and its encrypted password.

use Apine\Core\Encryption;

[...]

if (!SessionManager::is_logged_in()) {
    $encrypted_pwd = Encryption::hash_password($raw_password);
    if (SessionManager::login($username, $encrypted_pwd)) {
        // Login successful
    } else {
        // Login failed
    }
}

The logout() method allows to remove the 'logged in' state for the current session. In fact, it resets the session - removing all the data saved.

if (Session\SessionManager::is_logged_in()) {
    SessionManager::logout();
}

If a user is logged in you can then retrieve it using the get_user() method. The method will return an instance of Apine\User\User.

if (SessionManager::is_logged_in()) {
    $user = SessionManager::get_user();
}

Save Data to the Session

Session data are information saved into the session. Session data allows to transmit the information accross requests until the session expires. Session data are an implementation of the EntityModel. The session identifier is required to modify data stored in a session.

The class for the Session Data is named SessionData and is part of the Session module. To use the class include it in the header of your file.

use Apine\Session\SessionData;

To access the information stored in a session instantiate the class using optionaly the session identifier optained from the session manager as an argument.

$identifier = SessionManager::get_session_identifier();
$session_data = new SessionData($identifier);

Session Data are manly aimed at Web Session. In the case a web session is currently active, the identifier is optional. The SessionData class already knows the identifier of the current session.

The modification of the data stored is done using the methods get_var(), set_var() and remove_var(). get_var() is used to fetch a value of a stored in the session. set_var() stores a new value or modify an existing value. remove_var() is used to remove a value from the session.

// Add or modify a value
$session_data->set_set('code', 420);

// Fetch a value
$value = $session_data->get_var('code');

// Remove a value
$session_data->remove_data('code');

When modifications are done, don't forget to save, otherwise the new value will be lost at the end of the execution.

 $session_data->save();
Clone this wiki locally