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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.