ramesh_ps1 has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to retrive value from session using Perl CGI.
by leocharre (Priest) on Sep 11, 2007 at 13:49 UTC | |
I don't know where you got that code from, but it is some butt ugly code. The good news is that if you want to get down with perl, there are much more elegant places to see code. Have you seen the CGI::Session and CGI cpan pages? I suggest you print them out and staple them together. Have them besides you when you code. One tip: You have a lot of print statements thrown about here and there, this is mixing up feedback with logic. You should separate them. At one section of your code you should work out logic, what you are trying the computer to have do for you. There should be no 'result', feedback. Yet. You could store everything in a variable. ..
Which brings be to the other part... CGI can do a lot more then you think. And you should use that. Look up Ovid for more on that, and why. It may seem like using those tools instead of just using print is overkill- it's not. In the long run you'll code faster and solve problems. | [reply] [d/l] |
by ramesh_ps1 (Initiate) on Sep 12, 2007 at 10:02 UTC | |
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. | [reply] |
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.
here you can see what it does in action | [reply] [d/l] |