Apache-ASP
view release on metacpan or search on metacpan
site/articles/perlmonth2_build.html view on Meta::CPAN
needs to have. We have 2 pages, the intro, and the
actual bookmarks page, where we get to view, add, and
delete the bookmark entries. We have the user login to
the bookmarks, and logout, securing access for the user's
eyes only.
<p>
<center><img src=flow.gif border=0></center>
<p>
You might also design the objects, methods, and functions
that will be used for the site, but this site is so
simple, that we are going to jump into implementation.
<h3>Implementation</h3>
We start by configuring <tt>.htaccess</tt> file
of a directory in apache to allow Apache::ASP
to run <tt>.asp</tt> files, and testing the configuration
with a <tt>dummy.asp</tt> file.
<p>
<center>
<table border=0 cellspacing=0 width=90% >
<tr bgcolor=gray><td><font color=white><b># .htaccess</b></td></tr>
<tr bgcolor=#c0c0c0><td><pre>
<tt>DirectoryIndex index.asp
<Files ~ \.asp$>
SetHandler perl-script
PerlHandler Apache::ASP
PerlSetVar Global .
PerlSetVar GlobalPackage My::Bookmarks
PerlSetVar StateDir /tmp/asp_apps_bookmarks
PerlSetVar Debug 2
PerlSetVar SessionTimeout 15
PerlSetVar StatScripts 1
PerlSetVar AllowApplicationState 1
PerlSetVar AllowSessionState 1
</Files></tt></pre></td></tr>
</table>
</center>
<p>
<p>
<center>
<table border=0 cellspacing=0 width=90% >
<tr bgcolor=gray><td><font color=white><b># dummy.asp</b></td></tr>
<tr bgcolor=#c0c0c0><td><pre>
<tt>INTRO <%=$Session%></tt></pre></td></tr>
</table>
</center>
<p>
If the index.asp works on your server, and just prints
<tt>INTRO Apache::ASP::Session=HASH(0x??????)</tt>,
then we know Apache::ASP is working and $Sessions are
enabled.
<hr size=1>
Next, we set up the <tt>global.asa</tt> with globals and
libraries that need to be initialized for the web
application, and define the relevant event handlers.
We also set up per request globals, like the document's
title, which is something that we can do in
<tt>Script_OnStart</tt>. Finally, we use
the <tt>Script_OnStart</tt> and <tt>Script_OnEnd</tt>
events to automatically include the header and footer
for each script in our web application, and initialize
relevant globals used by the scripts.
<p>
Notice that each script can process its own <tt>Logout</tt>
request, which was a decision made after the design
because it seemed good to make the first script, <tt>index.asp</tt>,
<tt>$Session</tt> aware.
<p>
<center>
<table border=0 cellspacing=0 width=90% >
<tr bgcolor=gray><td><font color=white><b># global.asa</b></td></tr>
<tr bgcolor=#c0c0c0><td><pre>
<tt>use File::Basename;
use DBI;
use DBD::CSV;
use vars qw( $DarkColor $Name %Titles $FontBase $Db $Title $Basename $Form $Query );
$DarkColor = '#0000aa';
$Name = "MyBookmarks";
%Titles = (
'index.asp' => 'Introduction',
'bookmarks.asp' => 'Viewer'
);
$FontBase = 'face=verdana,arial';
$Db = DBI->connect("DBI:CSV:f_dir=".Apache->dir_config('StateDir'), '', '',
{ RaiseError => 1 })
or die "Cannot connect: " . $DBI::errstr;
# setup bookmark database if first time
unless(eval { $Db->do("select bookmark_id,username,title,url from bookmarks") }) {
eval { $Db->do("drop table bookmarks"); };
$Db->do(<<CREATE) || die("can't create table $DBI::errstr");
create table bookmarks (
bookmark_id varchar(15),
username varchar(30),
title varchar(60),
url varchar(120)
)
CREATE
;
}
$Db->do("select * from bookmarks")
|| die("can't do select against bookmarks: $DBI::errstr");
sub Script_OnStart {
$Basename = basename($0);
$Title = $Name.' / '.$Titles{$Basename};
$Response->Include('header.inc');
$Form = $Request->Form();
$Query = $Request->QueryString();
$Response->Expires(0);
# a user may logout from any script, destroy session, and go
# to login / intro page
if($Form->{logout}) {
$Session->Abandon();
$Response->Redirect("index.asp?abandon=".
++$Application->{abandon});
}
}
sub Script_OnEnd {
$Response->Include('footer.inc');
}
sub Application_OnStart {
# use max_bookmark_id as a pseudo sequence
$Application->Lock();
my $sth = $Db->prepare_cached
("select bookmark_id from bookmarks order by bookmark_id desc");
$sth->execute();
$Application->{max_bookmark_id} = $sth->fetchrow_array();
$Application->UnLock();
}</tt></pre></td></tr>
</table>
</center>
<p>
<hr size=1>
Next we set up the headers and footers for each page.
One problem with <tt>HTML</tt> is that it requires you to specify
the unique titles of the document before the standard
body style for your site, so we cheated this and
created the per page titles already in the <tt>Script_OnStart</tt>
of the <tt>global.asa</tt>.
<p>
<center>
<table border=0 cellspacing=0 width=90% >
<tr bgcolor=gray><td><font color=white><b># header.inc</b></td></tr>
<tr bgcolor=#c0c0c0><td><pre>
<tt><html>
<head><title><%=$Title%></title></head>
<body bgcolor=white link=purple alink=yellow vlink=gray>
<form src=<%=$Basename%> method=POST>
<table border=0 width=100% cellpadding=5 cellspacing=0>
<tr bgcolor=<%= $DarkColor %>>
<td>
<b><font <%=$FontBase%> size=+1 color=yellow>
<%=$Title%>
<% if($Session->{user}) { %>
for <%= $Session->{user} %>
<% } %>
</font></b>
</td>
<td align=right>
<font <%=$FontBase%>>
<% if($Session->{'user'}) { %>
<input type=submit name=logout value=Logout>
<% } else { %>
&nbsp;
<% } %>
</font>
</td>
</tr>
</form>
</table>
<table border=0 cellpadding=5 width=100% ><tr><td valign=top>
<font <%=$FontBase%> size=+0></tt></pre></td></tr>
</table>
</center>
<p>
<p>
<center>
<table border=0 cellspacing=0 width=90% >
<tr bgcolor=gray><td><font color=white><b># footer.inc</b></td></tr>
<tr bgcolor=#c0c0c0><td><pre>
<tt></font>
</table>
<table border=0 width=100% cellpadding=5>
<tr>
<td bgcolor=yellow align=center>
<font <%=$FontBase%> size=-1 color=<%= $DarkColor %>>
<b>
My-NotExists-Bookmarks
Cool Technologies Etc., ???, &copy; <%= (localtime())[5] + 1900 %>
</b>
</font>
</td>
( run in 1.020 second using v1.01-cache-2.11-cpan-39bf76dae61 )