Session guide
The Session module provides a session tracking and session storage mechanism.
When using the Session module, each visitor to your web app is assigned a random and secure unique identifier, and you'll be able to keep track of their activity across requests, and store visitor-specific data.
The most obvious use of a session mechanism like this is to implement a login system to let your users identify themselves with some sort of password, and give them access to their private sections and functionalities. This is exactly what the Login module does using Session.
But you can use the Session module for many other purposes. Let's see how.
Setting up the Session database table
The Session module uses the cherrycake_session
table in the database to store the sessions information.
You can create the Session table in your database by importing the
session.sql
file you'll find in the Cherrycake skeleton repository, under theinstall/database
directory.
Because Session needs a connection to a database, you need to set it but by creating a /config/Database.config.php
file just like we did in the Database guide.
Working with Session
First, let's remember our simple Hello world web app, which worked with this basic HelloWorld
app module:
Note we've updated the
show
method to use the HtmlDocument module to create the HTML document structure, now that we learned how it works in the HtmlDocument Guide.
Now, to use the Session module, you first need to add it to the list of your core module dependencies, like this:
Now, let's say we want to show how many times the visitor has seen the Hello World page. We'll do this by storing the views counter in the visitor's session, like this:
Now, every time a visitor reloads the page they'll see the counter growing:
See this example working in the Cherrycake documentation examples site.
Last updated