Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Regarding User Sessions

by Anneq (Vicar)
on Apr 12, 2004 at 11:37 UTC ( #344356=note: print w/ replies, xml ) Need Help??


in reply to Regarding User Sessions

CGI::Session works great for me.

Anne


Comment on Re: Regarding User Sessions
Re: Re: Regarding User Sessions
by Anonymous Monk on Apr 12, 2004 at 12:05 UTC
    could you please give an example program, so that i can use it as my reference. pls.......... Thank you
      Anneq, could you please give an example program, so that i can use it as my reference. pls.......... Thank you

      I am using CGI::Application in which my cgiapp_prerun() is as follows:

      sub cgiapp_prerun 
      {
      	my $self = shift;
      	my $q = $self->query();
      	
      	# Open existing session from cookie id, or open new session
      	my $session = new CGI::Session(undef, $q, {Directory=>'/tmp'});
      
      	# Delete session if user requested logout
      	if ($q->param('rm') eq 'logout') 
      	{
      		$session->delete();
                      # Start new session
      		$session = new CGI::Session(undef, undef, {Directory=>'/tmp'});
                      # Set session as logged out
      		$session->param(-name=> 'logged_in', -value => 0);
                      # Change run mode to default run mode
      		$self->prerun_mode('default'); 
      	} 
      	$session->expire('+1h');	
      				
      	my $cookie = $q->cookie(CGISESSID => $session->id);
              # Send cookie in header
      	$self->header_props(-cookie => $cookie);
      	# Make session params available to other subs & modules
      	$self->param(session => $session);
      }
      

      A separate validation run mode, which is used to validate both registrations and logins, sets the session parameter 'logged_in' to true if registration or logon was successful.

      Pretty simple and it works. I haven't checked out Apache::Session yet so I don't konw which one would be best to use.

      UPDATE:

      I've just came across this node which recommends using CGIS::Application because it's CGI::Session enabled CGI::Application. Though I haven't looked at it yet so can't give any opinion on which one would be best.

      HTH,

      Anne

        Hi Anne,

        Another option is to use CGI::Application::Session. It also provides seamless integration with CGI::Session. I use Apache::AuthCookieDBI to handle user authentication, but the logic in my cgiapp_prerun handler is similar (if user is not logged in, redirect to a runmode that displays login page).

        William
        Hi, im using CGI::Application too, but I don't know how to use sessions. I mean, this is the functionality i would like:

        1. if a user is unregistered, he just stays at the index page till he presses the register link, which directs him to a register runmode. Then when he registers, i add him to the database, his username,password and email. Then i redirect him to the login page, and show him a login form. When he logs in successfully, and checks a checkbox (make me a cookie or something) i want to create a session for him, where i store his session data in a mysql table.

        2. If he arrives at the index page and he's already registered and does have a cookie stored, i want to display a welcome msg to him and so on.

        So my question is, where would i put the different stuff? in what subs? would i put it in cgi_prerun? Because i don't want to automatically redirect the unregistered users to a register page when they come to the page, they'll have to click the register button first.

        and also, how do i store the session stuff in the mysql table? i mean, does the Driver:mysql take care of it? what if i want more fields? how do i store stuff in them? thank you

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-12-27 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls