# Development - Sessions

- Initializing a Session
- Session Preferences
- Adding Custom Session Data
- Retrieving Session Data
- Flashdata
- Saving Session Data to a Database
- Destroying a Session

### Initializing a Session ¶

In ApPHP Framework Sessions are typically run globally, so appropriate component is auto-loaded by the system. For the most part the session component will run unattended in the background, so you may read, update or delete session data without any special initial operations.

### Sessions Preferences ¶

You can find the following Session related preferences in your `protected/config/main.php` file:

    // Session settings
    'session' => array(
        'customStorage' => false, 	/* true value means use a custom storage (database), false - standard storage */
        'cacheLimiter' => '', 	/* to prevent 'Web Page expired' message for POST request use "private,must-revalidate" */
        'lifetime' => 24,  		/* session timeout in minutes, default: 24 min = 1440 sec */
    ),

### Adding Custom Session Data ¶

If you want to add/store data in a session, use the following syntax:

    A::app()->getSession()->set('some_name', 'some_value');

You may also save arrays in sessions. Below the example of such code:

    $userData = array(
        'username'  => 'johnsmith',
        'email'     => 'johnsmith@email.me',
        'logged_at' => date('Y-m-d H:i:s'),
        'logged_in' => true
    );

    A::app()->getSession()->set('some_name', $userData);

Sometimes to make your code more readable, you can make it more compact by placing `A::app()->getSession()` in a separate variable, that may be used over the rest of code.

    $cSession = A::app()->getSession();
    ...
    $cSession->set('some_name', 'some_value');

Remeber: Cookies can only hold 4KB of data, so be careful not to exceed the capacity. The encryption process in particular produces a longer data string than the original so keep careful track of how much data you are storing. For special cases you may use Custom Data Storage.

### Retrieving Session Data ¶

Any piece of information from the session is available using the following syntax:

    A::app()->getSession()->get('some_name');

For example, to fetch the `user_id` you will do this:

    $userId = A::app()->getSession()->get('user_id');
    // Alternative way
    $cSession = A::app()->getSession();
    $userId = $cSession->get('user_id');

### Flashdata ¶

ApPHP Framework supports co-called "flashdata", or session data that will be available on the next server request (or until you read it), and then automatically cleared. Flashdata can be very useful, typically for informational or status messages (for example: "record X has been deleted").
Note: Flash variables are postfaced with "_flash" to avoid name overlapping with your own session names.
To add flashdata:

    A::app()->getSession()->setFlash('delete_message', 'This record has been sucessfully deleted!');

To read a flashdata variable:

    $msg = A::app()->getSession()->getFlash('delete_message');

You may also check if flashdata exists:

    if(A::app()->getSession()->hasFlash('delete_message')){
        // Do something
    }

### Saving Session Data to a Database ¶

ApPHP Framework supports sacing sessions in custom storage, for example in database. You may read more about this in Session Custom Storage section.

### Destroying a Session ¶

To clear the current session:

    A::app()->getSession()->endSession();