Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Miguel (Friar)
on Apr 08, 2005 at 07:08 UTC ( #445962=note: print w/ replies, xml ) Need Help??


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

I wrote a small script in reply to your previous post. Perhaps you didn't read it.

In my opinion you should _not_ create a session or send a cookie to the user's browser if you don't need it. Doing that you're wasting resources on server, bandwidth, filling the browser's cache with junk and wasting your time.


Comment on Re: Code works fine, but in subroutine does not... help plz!
Re^2: Code works fine, but in subroutine does not... help plz!
by Cody Pendant (Prior) on Apr 08, 2005 at 08:00 UTC
    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
      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.
Re^2: Code works fine, but in subroutine does not... help plz!
by Stenyj (Beadle) on Apr 09, 2005 at 02:05 UTC
    I was reviewing your example. As far as I can read, that's simply a login/logout script. So one of the two actions much take place. What about my situation, where I don't want to login or logout, I simply want to check if they're logged in, and what is displayed depends on whether or not they are logged in or not. If they're not logged in, then a session should no be created, but I believe these lines:
    my $sessionid=$q->cookie("cgisessid")||$session->param("_SESSION_ID"); my $cookie_id = $q->cookie(CGISESSID=>$session->id);

    in your example both creat a session (regardless of whether the person is logged in or not) and then creates a cookie (again, regardless of whether or not they're logged in.

    or am I misreading the code?

    Thx for all your feedback guys, I really appreciate it!

    Stenyj

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-08-28 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (258 votes), past polls