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 %>