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

confusion over is_expired and is_empty in CGI:Session

100+
P: 170
hey guys,
i'm not sure if u guys have place this problem before

i did an expiration of my CGI session for 1 min

and i got the code below to detect
Expand|Select|Wrap|Line Numbers
  1. if($session->is_expired)
  2.   {
  3.       print $cgi->header(-cache_control=>"no-cache, no-store, must-revalidate");
  4.       print "<p>Your has session expired. Please login again.<p></br>";
  5.       print "<a href='http://localhost//cgi-bin//mml%20log//login.pl'>Login</a>";
  6.   }
  7.   elsif ($session->is_empty)
  8.   {
  9.  
  10.       print $cgi->header(-cache_control=>"no-cache, no-store, must-revalidate");
  11.       print "You have not logged in";
  12.   }
  13.  
the problem is i know when the session expires, it deletes the session off
now when it expires instead of printing "your session expired"
it keeps printing you have not logged in

so i'm wondering if there's anything wrong with the is_expired function
May 30 '08 #1
Share this Question
Share on Google+
7 Replies


eWish
Expert 100+
P: 971
is_expired()

Tests whether session initialized using load() is to be expired. This method works only on sessions initialized with load():
Are you using this function correclty?

--Kevin
Jun 5 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
Did you initialize the session using the load() function? If not, then using the is_expired() function is no use as it only works for sessions initialized with load();.

Regards,

Jeff
Jun 5 '08 #3

100+
P: 170
yup i did
ok let me show u my logi script
ignore the ##username## and ##password## portion, i need to mask it off
so this is my log in page

Expand|Select|Wrap|Line Numbers
  1. #!/perl/bin/perl
  2.  
  3. use strict;
  4. use DBI();
  5. use CGI;
  6. use CGI::Session(' ip_match ');
  7. use Digest::SHA1 qw(sha1);
  8.  
  9. my ($dbh, $sth, $name1, $name2, $session);
  10.  
  11. my $cgi = new CGI;
  12.  
  13. my $username = $cgi->param('username');
  14. my $password = $cgi->param('password');
  15.  
  16. if($username ne '')
  17. {
  18. $dbh = DBI->connect("DBI:mysql:database=authentication;host=localhost","##username##","##password##", 
  19.               {RaiseError => 1});
  20.  
  21. $sth = $dbh ->prepare("SELECT * FROM `example` WHERE Username='$username' AND Password=SHA1('$password')");
  22. $sth->execute();
  23. while (my $ref =$sth->fetchrow_hashref()) {
  24. $name1 = $ref->{'Username'}, $name2 = $ref->{'Password'};}
  25.  
  26.  
  27.  
  28. if ($username eq $name1 && ($username))
  29.     {
  30.        $session = new CGI::Session();
  31.        print $session->header(-location=> "http://localhost//cgi-bin//mml%20log//element_select.pl");
  32.     }
  33.     else
  34.     {
  35.     print
  36.     $cgi->header,
  37.     $cgi->start_html(-title=>'StarHub Login');
  38.  
  39. print <<HTML;
  40. <FORM METHOD="GET">
  41. <div style="height100%; width100%; z-index:-1; position:absolute; top:0px; bottom:0px">
  42. <IMG SRC="C:\\Program Files\\Apache Software Foundation\\Apache2.2\\
  43.                         htdocs\\starhub1024by768.jpg" ALT="starhub">
  44. </div>
  45. <div style="position:absolute; top:223px"> 
  46. <p>You have Entered an Incorrect password or username</p>
  47. <p>Please Log in below:</p>
  48. <p>Username: <INPUT TYPE=TEXT NAME="username"></p>
  49. <p>Password: <INPUT TYPE=PASSWORD NAME="password"></p></br>
  50. <INPUT TYPE=SUBMIT name=QUERY2 value="Log In"> 
  51. <a href="http://localhost//cgi-bin//mml%20log//passwordchange.pl">Forget your Password?</a>
  52. </div>
  53. </FORM>
  54. HTML
  55.  
  56. print $cgi->end_html();
  57.  
  58.     }
  59. $dbh->disconnect();
  60.  
  61. }
  62. elsif($cgi->param('action') eq 'logout')
  63.      {
  64.      $session = CGI::Session->load() or die CGI::Session->errstr;
  65.            $session->delete();
  66.            print $session->header(-location=>"http://localhost//cgi-bin//mml%20log//login.pl");
  67.      }
  68.      else
  69.      {    
  70.  
  71. print
  72.     $cgi->header,
  73.     $cgi->start_html(-title=>'StarHub Login');
  74.  
  75. print <<HTML;
  76. <FORM METHOD="GET">
  77. <div style="height100%; width100%; z-index:-1; position:absolute; top:0px; bottom:0px">
  78. <IMG SRC="C:\\Program Files\\Apache Software Foundation\\Apache2.2\\
  79.                         htdocs\\starhub1024by768.jpg" ALT="starhub">
  80. </div>
  81. <div style="position:absolute; top:223px"> 
  82. <p>Please Log in below:</p>
  83. <p>Username: <INPUT TYPE=TEXT NAME="username"></p>
  84. <p>Password: <INPUT TYPE=PASSWORD NAME="password"></p></br>
  85. <INPUT TYPE=SUBMIT name=QUERY2 value="Log In"> 
  86. </div>
  87. </FORM>
  88. HTML
  89.  
  90. print $cgi->end_html();
  91. }
  92.  
if login successful
i direct it to this script

Expand|Select|Wrap|Line Numbers
  1. #!/perl/bin/perl
  2.  
  3. use strict;
  4. use DBI();
  5. use CGI;
  6. use CGI::Session(' ip_match ');
  7.  
  8.  
  9. my $session;
  10.  
  11. my $cgi = new CGI;
  12. $session = CGI::Session->load();
  13.  
  14. my $mmloption = $cgi->param('mmloption');
  15.  
  16.  
  17. if($session->is_expired)
  18.   {
  19.       print $cgi->header(-cache_control=>"no-cache, no-store, must-revalidate");
  20.       print "<p>Your has session expired. Please login again.<p></br>";
  21.       print "<a href='http://localhost//cgi-bin//mml%20log//login.pl'>Login</a>";
  22.   }
  23.   elsif ($session->is_empty)
  24.   {
  25.  
  26.       print $cgi->header(-cache_control=>"no-cache, no-store, must-revalidate");
  27.       print "You have not logged in";
  28.   }
  29.   else
  30.   {
  31.  
  32.  
  33. if ($mmloption eq "1")
  34. {
  35.     print $cgi->header(-type=>"text/html",-location=>"http://localhost//cgi-bin//mml%20log//1//optionA.pl");
  36. }
  37. elsif ($mmloption eq "12") 
  38. {
  39.  
  40.     print $cgi->header(-type=>"text/html",-location=>"http://localhost//cgi-bin//mml%20log//12//optionA.pl");
  41. }
  42. elsif ($mmloption eq "123")
  43. {
  44.     print $cgi->header(-type=>"text/html",-location=>"http://localhost//cgi-bin//mml%20log//123//optionA.pl");
  45. }
  46.  
  47. elsif ($mmloption eq "1234")
  48. {
  49.     print $cgi->header(-type=>"text/html",-location=>"http://localhost//cgi-bin//mml%20log//1234//optionA.pl");
  50. }
  51. else
  52. {
  53.  
  54.  
  55.  
  56. print
  57.     $cgi->header,
  58.     $cgi->start_html(-title=>'Elements Selection');
  59.  
  60. print <<HTML;
  61.  
  62. <FORM METHOD="GET">
  63. <div style="height100%; width100%; z-index:-1; position:absolute; top:0px; bottom:0px">
  64. <IMG SRC="C:\\Program Files\\Apache Software Foundation\\Apache2.2\\
  65.                         htdocs\\starhub1024by768.jpg" ALT="starhub">
  66. </div>
  67. <div style="position:absolute; top:223px"> 
  68. <p>Please Select the elements below:</p>
  69. <INPUT TYPE=RADIO NAME="mmloption" VALUE="12">12<BR></br>
  70. <INPUT TYPE=RADIO NAME="mmloption" VALUE="1">1<BR></br>
  71.  
  72. <input type=submit name=Query3 value=Submit>
  73. <a href='http://localhost//cgi-bin//mml%20log//login.pl?action=logout'>Logout</a>
  74. </div>
  75.  
  76. HTML
  77.  
  78. print $cgi->end_html();
  79. }
  80. }
  81.  
hopefully i did it correctly
Jun 6 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
In your original post, you said you set the expiration to 1 minute. I looked through what you posted and I don't see it. I don't see where you used the expire() function to set the 1 minute time-out.

Regards,

Jeff
Jun 6 '08 #5

100+
P: 170
opps sorry

Expand|Select|Wrap|Line Numbers
  1. #!/perl/bin/perl
  2.  
  3. use strict;
  4. use DBI();
  5. use CGI;
  6. use CGI::Session(' ip_match ');
  7. use Digest::SHA1 qw(sha1);
  8.  
  9. my ($dbh, $sth, $name1, $name2, $session);
  10.  
  11. my $cgi = new CGI;
  12.  
  13. my $username = $cgi->param('username');
  14. my $password = $cgi->param('password');
  15.  
  16. if($username ne '')
  17. {
  18. $dbh = DBI->connect("DBI:mysql:database=authentication;host=localhost","##username##","##password##", 
  19.               {RaiseError => 1});
  20.  
  21. $sth = $dbh ->prepare("SELECT * FROM `example` WHERE Username='$username' AND Password=SHA1('$password')");
  22. $sth->execute();
  23. while (my $ref =$sth->fetchrow_hashref()) {
  24. $name1 = $ref->{'Username'}, $name2 = $ref->{'Password'};}
  25.  
  26.  
  27.  
  28. if ($username eq $name1 && ($username))
  29.     {
  30.        $session = new CGI::Session();
  31.         $session->expire("1m");
  32.        print $session->header(-location=> "http://localhost//cgi-bin//mml%20log//element_select.pl");
  33.     }
  34.     else
  35.     {
  36.     print
  37.     $cgi->header,
  38.     $cgi->start_html(-title=>'StarHub Login');
  39.  
  40. print <<HTML;
  41. <FORM METHOD="GET">
  42. <div style="height100%; width100%; z-index:-1; position:absolute; top:0px; bottom:0px">
  43. <IMG SRC="C:\\Program Files\\Apache Software Foundation\\Apache2.2\\
  44.                         htdocs\\starhub1024by768.jpg" ALT="starhub">
  45. </div>
  46. <div style="position:absolute; top:223px"> 
  47. <p>You have Entered an Incorrect password or username</p>
  48. <p>Please Log in below:</p>
  49. <p>Username: <INPUT TYPE=TEXT NAME="username"></p>
  50. <p>Password: <INPUT TYPE=PASSWORD NAME="password"></p></br>
  51. <INPUT TYPE=SUBMIT name=QUERY2 value="Log In"> 
  52. <a href="http://localhost//cgi-bin//mml%20log//passwordchange.pl">Forget your Password?</a>
  53. </div>
  54. </FORM>
  55. HTML
  56.  
  57. print $cgi->end_html();
  58.  
  59.     }
  60. $dbh->disconnect();
  61.  
  62. }
  63. elsif($cgi->param('action') eq 'logout')
  64.      {
  65.      $session = CGI::Session->load() or die CGI::Session->errstr;
  66.          $session->delete();
  67.          print $session->header(-location=>"http://localhost//cgi-bin//mml%20log//login.pl");
  68.      }
  69.      else
  70.      {  
  71.  
  72. print
  73.     $cgi->header,
  74.     $cgi->start_html(-title=>'StarHub Login');
  75.  
  76. print <<HTML;
  77. <FORM METHOD="GET">
  78. <div style="height100%; width100%; z-index:-1; position:absolute; top:0px; bottom:0px">
  79. <IMG SRC="C:\\Program Files\\Apache Software Foundation\\Apache2.2\\
  80.                         htdocs\\starhub1024by768.jpg" ALT="starhub">
  81. </div>
  82. <div style="position:absolute; top:223px"> 
  83. <p>Please Log in below:</p>
  84. <p>Username: <INPUT TYPE=TEXT NAME="username"></p>
  85. <p>Password: <INPUT TYPE=PASSWORD NAME="password"></p></br>
  86. <INPUT TYPE=SUBMIT name=QUERY2 value="Log In"> 
  87. </div>
  88. </FORM>
  89. HTML
  90.  
  91. print $cgi->end_html();
  92. }
  93.  
  94.  
sorry i added it in
i tried with that but it still didn;t work
Jun 6 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
Unfortunately, I don't know why its not working then. The use would seem correct, but I just don't know. Wish I could help more, sorry.

Regards,

Jeff
Jun 6 '08 #7

100+
P: 170
hm...all i know is if a session expires
the session is deleted and becomes empty
perhaps that;s why it always detect it is an empty session
i've read some issues on this, apparently it's said to be a bug in the module
but no proven evidence yet
Jun 6 '08 #8

Post your reply

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