in reply to Re: How to retrive value from session using Perl CGI.
in thread How to retrive value from session using Perl CGI.

Hi, Thanks for the suggestion.

I have modified the code based on your suggestion.
Now my first page cookie1.cgi looks like this.


#!/usr/bin/perl -w
use CGI;
use CGI::Session;
use DBI;
#my $sid = $q->cookie('SessionId') || $q->param('SessionId') || undef;
my $db_name="db";
my $db_host="dbhost";
my $db_user="dbuser";
my $db_pass="dbpass";
# Prepare Database connection to store session values
$dbh = DBI->connect("DBI:mysql:database=$db_name;
host=$db_host","$db_user","$db_pass", {PrintError => 1, RaiseError => 1, AutoCommit => 1});

my $q = new CGI;

# Create new session vareable
my $session = new CGI::Session("driver:MySQL;
id:MD5", undef, {Handle=>$dbh});
$session->param(-name =>'userid',-value => 'ramesh');

$session->expire(5000);

$cookie = $q->cookie(-name =>'SessionId',-value => $session->id );

print $q->header(-cookie=>$cookie),
$q->h3("Cookie contains : $cookie "),
$q->start_html, $q->start_form(-name=>'testcookie',-method=>'post',-action=>'./cookie2.cgi'),
$q->submit(-name=>'submit',-value=>'click to go to next page'),
$q->end_form, $q->end_html;
$dbh->disconnect();
exit;


I am able to store session ID in the cookie and retrive the same in the second page cookie2.cgi.
Problem is, I am not able to retrive session data based on session ID obtained from cookie.
I am trying to load session as follows,

Session ID is obtained from cookie
my $sid = $q->cookie('SessionId') || $q->param('SessionId') || undef;

Load session and connect to mysql
$session=CGI::Session->load("driver:MySQL;id:MD5", undef, {Handle=>$dbh});

Assign session ID to session
$session->param("id", $sid);

Am I doing it correct?

Please let me know how to access the session data of a specefic session based on the session ID stored in the cookie.

  • Comment on Re^2: How to retrive value from session using Perl CGI.

Replies are listed 'Best First'.
Re^3: How to retrive value from session using Perl CGI.
by leocharre (Priest) on Sep 12, 2007 at 20:04 UTC

    Ok, what I am going to show you may freak you out. But that's ok.

    This is not meant to overwhelm you. I hope this is more of an inspiration of what perl can really do for you

    What you are learning is very useful. I've gone through those motions like most of us here have. But also, you should have a hint of what's at the end of the rainbow..

    So.. here. I wrote this just for you. I'll come back later and explain a little more. This code is fully tested and functional.

    package CGI::Application::Tutorial::Namegame; use base 'CGI::Application'; use CGI::Application::Plugin::Session; use CGI::Application::Plugin::Feedback ':all'; use CGI::Application::Plugin::AutoRunmode; use CGI::Application::Plugin::Forward; use strict; use warnings; use Carp; sub setup { my $self = shift; $self->start_mode('choose_name'); } sub choose_name : Runmode { my $self = shift; $self->_detect_name_change_request; my $q = $self->query; my $html = $q->start_html . $self->_nav . $q->h1('Choose Name') . $q->start_form . $q->textfield('name') . $q->submit('save') . $q->end_form . $q->end_html; return $html; } sub show_verse : Runmode { my $self = shift; $self->_detect_name_change_request; my $name = $self->_get_name_chosen or return $self->forward('choose_name'); require Lingua::EN::Namegame; my $verse = Lingua::EN::Namegame::name2verse($name) or $self->feedback("Sorry, cannot generate verse for [$name]") and $self->session->clear('name') and return $self->forward('choose_name'); my $q = $self->query; my $html = $q->start_html . $self->_nav . $q->h1('Your Verse:') . $q->pre($verse) . $q->end_html; return $html; } sub _detect_name_change_request { my $self = shift; my $name = $self->query->param('name'); defined $name or return 0; $name=~/^\w+$/ or $self->feedback('Your "name" input sucks.') and $self->feedback('Try again.') and return 0; $self->session->param( name => $name ); $self->session->flush; $self->feedback("Ok, name choice saved for [$name]."); return 1; } sub _get_name_chosen { my $self = shift; my $name = $self->session->param('name'); defined $name or $self->feedback('no name chosen yet') and return; return $name; } sub _nav { my $self = shift; my $navetc = $self->query->p(q{<a href="?rm=choose_name">Choose Name</a>}) . $self->query->p(q{<a href="?rm=show_verse">Show Verse</a>}) . $self->get_feedback_html; return $navetc; } 1;

    here you can see what it does in action