By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

how session tracking is done in perl?

100+
P: 170
hey guys,

i've done most of my web app. for searching almost done
but then i got a small little problem with logging in
i need to know how session tracking is done in perl
if not my log in page would truely be redundant
May 29 '08 #1
Share this Question
Share on Google+
2 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
hey guys,

i've done most of my web app. for searching almost done
but then i got a small little problem with logging in
i need to know how session tracking is done in perl
if not my log in page would truely be redundant
Well, to manage the session you could use the CGI::Session Perl module.

Regards,

Jeff
May 29 '08 #2

100+
P: 170
thanks..
i've actually found a workable code

right this code is for logging in
Expand|Select|Wrap|Line Numbers
  1. #!/perl/bin/perl
  2.  
  3. use CGI;
  4. use CGI::Session (' ip_match ');
  5.  use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
  6.  
  7. $q = new CGI;
  8.  
  9. $usr = $q->param('usr');
  10. $pwd = $q->param('pwd');
  11.  
  12. if ($usr ne '')
  13. {
  14. if($usr eq "demo" and $pwd eq "demo")
  15.       {
  16.           $session = new CGI::Session();
  17.           print $session->header(-location=>'index.pl');
  18.       }
  19.        else
  20.       {
  21.           print $q->header(-type=>"text/html",-location=>"login.pl");
  22.       }
  23. }
  24. elsif($q->param('action') eq 'logout')
  25.   {
  26.       $session = CGI::Session->load() or die CGI::Session->errstr;
  27.       $session->delete();
  28.       print $session->header(-location=>'login.pl');
  29.   }
  30.   else
  31.   {
  32.     print $q->header;
  33. print <<HTML;
  34.  <form method="post">
  35.           Username: <input type="text" name="usr">
  36.  
  37.           Password: <input type="password" name="pwd">
  38.  
  39.  
  40.           <input type="submit">
  41.  
and the other is for validating at the top of each cgi script
Expand|Select|Wrap|Line Numbers
  1. #!/perl/bin/perl
  2.  
  3.   # index.pl
  4.   use CGI;
  5.   use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
  6.   use CGI::Session ( '-ip_match' );
  7.  
  8.   $session = CGI::Session->load();
  9.   $q = new CGI;
  10.  
  11.   if($session->is_expired)
  12.   {
  13.       print $q->header(-cache_control=>"no-cache, no-store, must-revalidate");
  14.       print "Your has session expired. Please login again.";
  15.  print "<br/><a href='text.pl>Login</a>";
  16.   }
  17.   elsif($session->is_empty)
  18.   {
  19.       print $q->header(-cache_control=>"no-cache, no-store, must-revalidate");
  20.       print "You have not logged in";
  21.   }
  22.   else
  23.   {
  24.       print $q->header(-cache_control=>"no-cache, no-store, must-revalidate");
  25.       print "<h2>Welcome";
  26.       print "<a href='test.pl?action=logout'>Logout";
  27.   }
  28.  
i'm just wondering
i i were to write the validation code at the top each time..
it's so troublesome
is there any way i can call this script or anything
so that i dun have to always repeat the whole chunk of code?
May 30 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.