Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Code works fine, but in subroutine does not... help plz!

by Cody Pendant (Prior)
on Apr 08, 2005 at 08:00 UTC ( [id://445966]=note: print w/replies, xml ) Need Help??


in reply to Re: Code works fine, but in subroutine does not... help plz!
in thread Code works fine, but in subroutine does not... help plz!

Yes I'm still confused by how this is supposed to work.

Here's how it's supposed to be, surely:

  • on every page, you check whether there's a session logged in or not
  • depending whether there is one, you do whatever you need to do
  • only on the login page do you create a session
  • only on the logout page do you delete a session (sessions expire after a certain time anyway)

I think there's a fundamental problem with the way you're trying to do this.



($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print
  • Comment on Re^2: Code works fine, but in subroutine does not... help plz!

Replies are listed 'Best First'.
Re^3: Code works fine, but in subroutine does not... help plz!
by Stenyj (Beadle) on Apr 09, 2005 at 01:58 UTC
    Here's some sample scripts so it's clearer what I'm attempting to do & how everything works:

    Login script:
    #!c:/apache/perl/bin/perl.exe -wT use CGI; use strict; use CGI::Session; use HTML::Template; use MyAppCommon; my $foo = new CGI; my $alias = $foo->param('alias'); my $userID = 123; #normally would be pulled from DB, but keeping the c +ode as simple as possible for testing purposes my $empty; my $session = new CGI::Session(undef, $empty, {Directory=>"c:/apache/s +essions"}); $session->expire('+18h'); $session->param("id", $userID); $session->param("alias", $alias); my $cookie = $foo->cookie(-name => 'main', -value => $session->id, -expires => '+18h', -path => '/'); print $foo->header(-cookie => $cookie); MyAppCommon::topPublic(); my $template=HTML::Template->new(filename=>"c:/apache/templates/main/l +ogin.tmpl"); $template->param(ALIAS => $alias); print $template->output; MyAppCommon::bottomPublic($alias);

    One of the many pages that contain very similar coding (a page that simply wants to check if the user is logged in or not, and does NOT want to log them in if they're not... ie. does NOT create a session if they're not logged in... it only wants to check if they are & retrieve relevant session variables if they are logged in:

    #!c:/apache/perl/bin/perl.exe -wT use CGI; use strict; use CGI::Session; use HTML::Template; use MyAppCommon; my $foo = new CGI; print $foo->header(); my $sid = cookie('main') || undef; my @sessionInfo; my $session = new CGI::Session(undef, $sid, {Directory=>'c:/apache/ses +sions'}); if ( $session->is_new() ) { $session->delete(); } MyAppCommon::topPublic(); my $template=HTML::Template->new(filename=>"c:/apache/templates/main/h +ome.tmpl"); print $template->output; MyAppCommon::bottomPublic($alias, $userAccess, $membership);

    IF I comment out the following code of the above script:
    my $sid = cookie('main') || undef; my @sessionInfo; my $session = new CGI::Session(undef, $sid, {Directory=>'c:/apache/ses +sions'}); if ( $session->is_new() ) { $session->delete(); }

    and replace it with:
    &test; sub test { my $sid = cookie('main') || undef; my @sessionInfo; my $session = new CGI::Session(undef, $sid, {Directory=>'c:/apache +/sessions'}); if ( $session->is_new() ) { $session->delete(); } return 1; }
    I get the error I mentioned.

    " on every page, you check whether there's a session logged in or not # depending whether there is one, you do whatever you need to do"

    This is exactly what I want to do. Using the sessions ID, I want to determine if one exists with that session ID and to *NOT* create one if it doesn't exist.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://445966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-24 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found