Re: [Cgi-session-user] CGI::Session does not seem to be working for me
Brought to you by:
sherzodr
From: Cees H. <ce...@gm...> - 2005-09-13 03:19:19
|
On 9/12/05, Sherzod Ruzmetov <she...@ha...> wrote: > Vahid > P.S. You would discover this eventually, but you should look into > CGI::Application and HTML::Template (Or Template Toolkit). The way you're > doing things right now (with if/else, and embedded 'HTML'), you can only = go > so far. I thought I'd take the opportunity to show how easy this can actually be done, and how CGI::Application can really simplify your web apps.=20 And also if I may, I'll take a second to show off the new Authentication plugin for CGI::Application (which can use CGI::Session to keep things on topic). The new Authentication plugin will be on CPAN by the end of the week, but a preview version is available here:=20 http://cees.crtconsulting.ca/perl/modules/CGI-Application-Plugin-Authentica= tion-0.01.tar.gz Also, I should note that there is no LDAP driver yet, but it is very easy to build new authentication drivers (and LDAP is a planned addition). For simplicity, just put all the following files into a web enabled directory and call myapp.cgi from a web browser: -------------------------------------- myapp.cgi -------------------------------------- #!/usr/bin/perl use strict; use warnings; use lib qw/./; use MyApp; MyApp->new->run; -------------------------------------- MyApp.pm -------------------------------------- package MyApp; use base qw/CGI::Application/; use CGI::Application::Plugin::Session; use CGI::Application::Plugin::AutoRunmode; use CGI::Application::Plugin::Authentication; MyApp->auth->config( #DRIVER =3D> [ 'LDAP', DSN =3D> $dsn, PASSWORD =3D> $password ], DRIVER =3D> [ 'Generic', { user1 =3D> '123', user2 =3D> '1234' } ], STORE =3D> 'Session', LOGOUT_RUNMODE =3D> 'start', ); sub setup { my $self =3D shift; $self->tmpl_path('./'); $self->auth->protected_runmodes('protected'); } sub start : StartRunmode { my $self =3D shift; my $session =3D $self->session; my $template =3D $self->load_tmpl('start.tmpl'); $template->param( id =3D> $session->id, new_session =3D> $session->is_new ); return $template->output; } sub protected : Runmode { my $self =3D shift; my $session =3D $self->session; my $auth =3D $self->auth; my $template =3D $self->load_tmpl('protected.tmpl'); $template->param( id =3D> $session->id, username =3D> $auth->username, ); return $template->output; } 1; -------------------------------------- start.tmpl -------------------------------------- <html> <body> <h2>Start page</h2> <p>Session ID: <TMPL_VAR id></p> <p>New Session: <TMPL_IF new_session>Yes<TMPL_ELSE>No</TMPL_IF></p> <p><a href=3D"?rm=3Dprotected">View Protected Page</a></p> </body> </html> -------------------------------------- protected.tmpl -------------------------------------- <html> <body> <h2>Protected page</h2> <p>Session ID: <TMPL_VAR id></p> <p>Username: <TMPL_VAR username></p> <p><a href=3D"?rm=3Dstart">View Start Page</a></p> <p><a href=3D"?auth_logout=3D1">Logout</a></p> </body> </html> There are lots of other things you can do to simplify things (for example using template page headers), and also I would personally use Template Toolkit, but this should be a good showcase of some of the things that can be done with CGI::Application. Cheers, Cees |