Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

CGI::kSession example

by oubiwann (Sexton)
on Mar 14, 2002 at 23:05 UTC ( [id://151854]=CUFP: print w/replies, xml ) Need Help??

I have been helping the author of CGI::kSession write an updated pod, and as a result, offered to add an example of usage. Someone might find it useful (who knows how long before it hits CPAN...)
Sessions, URLs, and Cookies
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 ma +y 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 t +he # second click will retrieve a value from the cookie print "<a href=".(split/\//,$0)[-1]."?SID=$id>Next page</a>"; print $cgi->end_html();

Replies are listed 'Best First'.
Re: CGI::kSession example
by oubiwann (Sexton) on Mar 15, 2002 at 12:53 UTC
    Someone said they wanted to know why I hadn't posted CGI::kSession. Last I checked, it was on CPAN. Is it not there anymore? I'll check...

    Yup, you can still get it here:
    CGI::kSession
    CGI::kSession

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://151854]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-20 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found