|
CGI::kSession - sessions manager for CGI |
CGI::kSession - sessions manager for CGI
kSession.pm v 0.5.3
0.5.3
- updated the documentation
0.5.2
- fix value containing '='
0.5.1
- ugly fix with existing session
0.5
- lifetime
- path,lifetime as arguments with new
This module can be used anywhere you need sessions. As a session management module, it uses files with a configurable lifetime to handle your session data. For those of you familiar with PHP, you will notice that the session syntax is a little bit similar.
The following public methods are availible:
lifetime - how long the session lasts, in seconds
path - the directory where you want to store your session files
id - if you want to give the session a non-random name, use this parameter as well
use strict; use CGI; use CGI::kSession;
my $cgi = new CGI;
print $cgi->header;
my $s = new CGI::kSession(lifetime=>10,path=>"/home/user/sessions/",id=>$cgi->param("SID"));
$s->start();
# $s->save_path('/home/user/sessions/');
# registered "zmienna1"
$s->register("zmienna1");
$s->set("zmienna1","wartosc1");
print $s->get("zmienna1"); #should print out "wartosc1"
if ($s->is_registered("zmienna1")) {
print "Is registered";
} else {
print "Not registered";
}
# unregistered "zmienna1"
$s->unregister("zmienna1");
$s->set("zmienna1","wartosc2");
print $s->get("zmienna1"); #should print out -1
$s->unset(); # unregister all variables
$s->destroy(); # delete session with this ID
Marcin Krzyzanowski
use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use CGI::kSession; use CGI::Cookie;
my $cgi = new CGI;
my $last_sid = $cgi->param("SID");
my $c = new CGI::Cookie(-name=>'SID',-value=>$last_sid);
my ($id, $key, $value);
my $s = new CGI::kSession(path=>"/tmp/");
print $cgi->header(-cookie=>$c);
print $cgi->start_html();
if ($last_sid) {
# note: the following I used for mozilla - your mileage may vary
my $cookie_sid = (split/[=;]/, (fetch CGI::Cookie)->{SID})[1];
if ($cookie_sid) {
print "<b>We are now reading from the cookie:</b><p>";
$id = $s->id($cookie_sid);
$s->start($cookie_sid);
print "The cookie's id: $cookie_sid<br>";
print "Here's the test_value: ".$s->get("test_key")."<br>";
} else {
print "<b>We are now reading from the URL parameters:</b><p>";
$id = $s->id($last_sid);
$s->start($last_sid);
print "Last page's id: $last_sid<br>";
print "Here's the test_value: ".$s->get("test_key")."<br>";
}
} else {
print "<b>Here we will set the session values:</b><p>";
$s->start();
$id = $s->id();
print "My session id: $id<br>";
$s->register("test_key");
$s->set("test_key", "Oh, what a wonderful test_value this is...");
print "Here's the test_value: ".$s->get("test_key")."<br>";
}
# note: the first click will set the session id from the URL the
# second click will retrieve a value from the cookie
print "<a href=".(split/\//,$0)[-1]."?SID=$id>Next page</a>";
print $cgi->end_html();
Duncan McGreggor
Copyright 2000-2002 Marcin Krzyzanowski
Marcin Krzyzanowski <krzak at linux.net.pl> http://krzak.linux.net.pl/
Duncan McGreggor <oubiwann at yahoo.com>
|
CGI::kSession - sessions manager for CGI |