RudeServer Professional C++ CGI Development Libraries
Session Library Logo

   C++ Session Library - Basic Usage

 

RudeServer Home
Introduction
Download
Installation
Basic Usage
Compiling
API Documentation
Tutorials/Examples
License/Disclaimer
Discussion Forum
Acknowledgments
Revision History

 

Basic Usage Example
#include <rude/session.h>

using namespace rude;

int main(void)
{

        // Set Up Config Persistance
        //
        Session::setPersistanceMethod("config");
        Session::setSessionDirectory("/tmp/sessions");

        // Configure cookie information
        //
        Session::setPath("/");
        Session::setDomain(".example.com");
        Session::setCookieIdentifer("SESSIONID");

        //////////
        // Once the Session has been configured (as above), 
        // the following code can appear anywhere 
        // in your application
        //////////
        
        // You need to obtain the session instance *before*  
        // you output anything, because new sessions need to 
        // send an HTTP cookie header
        //
        Session *session = Session::getSession();

        // Use the session to store info
        //
        session->addValue("color", "red");

        // Save the changes
        //
        session->save();

        // Use the session to retrieve info
        //
        const char *color=session->getValue("color");

        // Clean up everything.  This should only happen
        // at the end of your program.
        //
        delete session;

}

Example using Database Persistance
#include <rude/session.h>
#include <rude/database.h>

using namespace rude;

int main(void)
{

        // We are going to use the following database connection, 
        // and we will identify it by "my_session_context", 
        // "which is arbitrarily determined
        //
        const char *contextname="my_session_context";
        const char *database="test";
        const char *username="some_mysql_user";
        const char *password="some_password";
        const char *server="localhost";
        int port=3306;

        // Create the database context
        //
        Database::addContext(contextname,database, username, password, server, port);
         
        // Now set up the session object to use the database context we have created
        //
        Session::setPersistanceMethod("database");
        Session::setDatabaseContext(contextname);

        // Configure cookie information
        //
        Session::setPath("/");
        Session::setDomain(".example.com");
        Session::setCookieIdentifer("SESSIONID");

        //////////
        // Once the Session has been configured (as above), 
        // the following code can appear anywhere 
        // in your application
        //////////
        
        // You need to obtain the session instance *before*  
        // you output anything, because new sessions need to 
        // send an HTTP cookie header
        //
        Session *session = Session::getSession();

        // Use the session to store info
        //
        session->addValue("color", "red");

        // Save the changes
        //
        session->save();

        // Use the session to retrieve info
        //
        const char *color=session->getValue("color");

        // Clean up everything.  This should only happen
        // at the end of your program.
        //
        delete session;

}

 

 

Copyright © 2000-2007 RudeServer

Site maintained by Matt Flood & Hermann West

Last Updated June 08, 2006