Diesel-Engine Documentation

Session Hash    back to index
Client Persistent Data

Client Persistent Data    back to top

There is a special hash table %session that can be used to store Client Persistent Data. Every browser that accesses a Diesel page is assigned a unique identifier as a HTTP Cookie. This identifier makes it possible to recognize a visitor and automatically associate stored data with that user.

Having the %session hash makes it very easy to "carry" information from page to page without having to worry about making hidden form fields or putting the data in every link. To put data in the %session hash is just as simple as using any other hash:

<% $session{'userid'} = $userid %>

The %session hash is ideal to use when implementing a application based authentication system (as opposed to a HTTP authentication scheme). The next example consists of 2 Diesel files:

login.dsl - A self contained Diesel page that will authenticate users based on a Mysql database.
content.dsl - A typical password protected content page that uses the users information to customize the page. The 3 lines at the top of the file forces a user to be logged in before displaying.

In the example we are assuming that the table users exists in the userdb database. The users table contains at least the fields: login, pass, name and email. Here is login.dsl:

<% $showform = 1 %>
<% if $login ne "" && $pass ne "" %>

<% sql connect="SRC=mysql;UID=nobody;PWD=;DB=userdb",
query="select * from users where
login = '$login' and pass ='$pass'" %>

<% if $sql_rset_total_rows == 1 %>

Welcome back <% echo $name %>.
<% $session{username} = $login %>
<% $session{name} = $name %>
<% $session{email} = $email %>
<% $showform = 0 %>

<% else %>

Authentication failed. Please try again.<p>

<% endif %>

<% endif %>

<% if $showform %>

Enter Your Login and Password:<p>
<form action=login.dsl method=post>
Login: <input name=login size=10>
Password: <input name=pass type=password size=10>
<input type=submit value=Login>
</form>

<% endif %>

The next file is a typical password protected page, content.dsl. The first 3 lines check to see if the user is logged in, if not, send the user to the login page.

<% if $session{login} eq "" %>
<% http_redirect 'login.dsl' %>
<% endif %>

<-- Password protected content -->

Welcome back <% echo $session{name} %>.

Today's Top Stories ...

Copyright © 2005 Blueprint Networks, Inc.