Main  /  Edit  /  History  /   /  Users Area

Starting With Sessions


In this tutoring we would show the basics of using sessions in CppCMS --- how to store connection persistent data in stateless HTTP protocol.


Our current code would be based on start with forms code.

Templates and Content

We would save the information about user using session_interface object that is a member of application. This information would be displayed every time it visits the page.

First of all let's change our "data.h": Instead of showing person's sex and state we should display a prefix "Mr", "Miss" or "Mrs" according to our saved parameters.

So lets change line:

string name,state,sex;


string name,who;

And we would use following template:

<h1>Hello <% who %> <% name %></h1>
<% if (content.age != -1.0) %>
    <p>Your age is <% age %></p>
    <h2>Change details</h2>
<% else %>
    <h2>Input your details</h2>
<% end %>
<form method="post" action="" >
<% form as_p info %>

Notes: We can inject arbitrary C++ code into conditions between two brackets () (line 2). However, now we should refer to all content class variables using content member. Templates engine would not substitute correct prefix for you, because it is native C++ code.

Saving data

Now let's rewrite out hello::info function:

First of all let's load new data from the form when it submitted to session object, instead of content. This information is preserved withing different requests:

data::message c;
if(env->getRequestMethod()=="POST") {*cgi);
        if( {


Now, our session data would be preserved between requests and we can fetch it:

Fetching Data

if(session.is_set("name")) {["name"];
    if(session["sex"]=="Male") {
    else {
            if(session["state"]=="Single") {
            else {
else {"Visitor";


Configuration --- behind the scenes

Storage Backend

CppCMS has several options to session management. In every case cookies are used --- there is no "GET" or "POST" methods for storing session information like /page/?sid=2e7f60c43b88d4b554a.

The developer has several options to save information:

We would add following lines to our CppCMS configuration file:

session.location = "client" 
session.cookies_key = "10b74ef35431d95c420dc67b52b3f166"


Duration Options

You can define several session duration options:

Each type of expiration has its time limit (even browser one has its limit). It can be defined using session.timeout parameter. The default one is 24 hours.

For example:

session.expire = "renew"
session.timeout = 604800 # Week

Hi. I have some suggestions: 1)For the tutorial, put all codes which are supposed to appear together in one cpp file or header file together at the end of the tutorial and make sure that line numbers do not start from 1. 2. 3. again in each section of tutorial. Currently, it is not clear where a function or a file begins and ends because there are broken pieces of codes and all being with 1. 2. 3. 2)I am not familiar with web programing. I think people like me prefer to use cpp files to wrap html codes or probably there are good html "designer", like GUI designers, that produces the code and I can copy-paste for the "view" in CMV.


Main Page

Valid CSS | Valid XHTML 1.0