Skip to content
This is the documentation for pieceofcake2/cakephp, a community-maintained fork of CakePHP 2.x for PHP 8. About this fork.

Sessions ​

class SessionComponent(ComponentCollection $collection, array $settings = [])

The CakePHP SessionComponent provides a way to persist client data between page requests. It acts as a wrapper for $_SESSION as well as providing convenience methods for several $_SESSION related functions.

Sessions can be configured in a number of ways in CakePHP. For more information, you should see the Session configuration documentation.

Interacting with Session data ​

The Session component is used to interact with session information. It includes basic CRUD functions as well as features for creating feedback messages to users.

It should be noted that Array structures can be created in the Session by using dot notation. So User.username would reference the following:

['User' => [
    'username' => 'clark-kent@dailyplanet.com'
]];

Dots are used to indicate nested arrays. This notation is used for all Session component methods wherever a name/key is used.

method SessionComponent::write($name, $value)

method SessionComponent::read($name)

method SessionComponent::consume($name)

method SessionComponent::check($name)

method SessionComponent::delete($name)

method SessionComponent::destroy()

Creating notification messages ​

method SessionComponent::setFlash(string $message, string $element = 'default', array $params = [], string $key = 'flash')

Based on the CakePHP Cookbook. Content licensed under CC BY-NC-SA 4.0. Not affiliated with the Cake Software Foundation.