## 1.11.13 Example

Below is an example demonstrating the full way to use the module. If you do not need this level of control over the `web.session` module you can direcly use the `web.session.start()` method. An example of the same code written in this simpler way was shown earlier in the documentation.

Here is a full example showing the creation of all the necessary objects and giving you full control over the session:

    #!/usr/bin/env python

    # show python where the modules are
    import sys; sys.path.append('../'); sys.path.append('../../../')
    import web.error; web.error.enable()
    import os, time
    import web.database

    # Setup a database connection
    connection = web.database.connect(
        adapter="snakesql",
        database="webserver-session",
        autoCreate = 1,
    )
    cursor = connection.cursor()

    # Create a session driver and make sure the database tables exist
    import web.session
    driver = web.session.driver('database', environment='testEnv', cursor=cursor)
    if not driver.completeSessionEnvironment():
        driver.removeSessionEnvironment(ignoreErrors=True)
        driver.createSessionEnvironment()

    # Set up a session manager and load or create a new session
    manager = web.session.manager(driver=driver, expire=10)
    sessionID = manager.cookieSessionID()
    if not manager.load(sessionID):
        manager.create(sendCookieHeaders=False)
        manager.sendCookieHeaders()

    # Get the session store for this application
    store = manager.store('testApp')

    def printPage(title, url, link, url2, link2, data):
        print """
        <html>
        <h1>%s</h1>
        <p><a href="%s">%s</a></p>
        <p><a href="%s">%s</a></p>
        <p>%s</p>
        </html>"""%(title, url, link, url2, link2, data)

    # Write a simple application
    if not manager.created:
        if web.cgi.has_key('destroy') and web.cgi['destroy'].value == 'True':
            manager.destroy(ignoreWarning=True, sendCookieHeaders=False)
            manager.sendCookieHeaders()
            print web.header('text/html')
            printPage(
                'Session Destroyed',
                os.environ['SCRIPT_NAME'],
                'Start Again', '','',''
            )
        else:
            manager.setExpire(manager.expireTime+5)
            manager.setCookie(sendCookieHeaders=True)
            print web.header('text/html')
            data = []
            data.append('SessionID:  ' +manager.sessionID)
            data.append('Store Keys: '+str(store.keys()))
            data.append('Store App:  '+store.app)
            data.append('Variable1:  '+str(store['Variable1']))
            data.append('ExpireTime: '+str(manager.expireTime))
            printPage(
                'Welcome back',
                os.environ['SCRIPT_NAME'],
                'Visit Again',
                os.environ['SCRIPT_NAME']+'?destroy=True',
                'Destroy Session',
                '<p>Every time you visit this page the expiry \
                time increases 5 seconds</p>'+'</p><p>'.join(data)
            )
    else:
        print web.header('text/html')
        store['Variable1'] = 'Python Rules!'
        printPage(
            'New Session Started',
            os.environ['SCRIPT_NAME'],
            'Visit Again', '', '',
            "Set variable1 to 'Python Rules!'"
        )

    connection.commit() # Save changes
    connection.close()  # Close the database connection

You can test this example by running the webserver scripts/webserver.py and visiting http://localhost:8080/doc/src/lib/webserver-web-session.py