Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

tracking session:variables

by rjsaulakh (Beadle)
on Jun 01, 2005 at 09:55 UTC ( [id://462377]=perlquestion: print w/replies, xml ) Need Help??

rjsaulakh has asked for the wisdom of the Perl Monks concerning the following question:

I have a script successfully running for CGI::Session . i am able to get the store my session variables every time my script runs.

what i want to implement is when user goes on the next page i should be able to get his userid. i want his userid as i have to perform some actions in the database in relation to his userid .

can you please help me how do i move with it . what concept should i apply . i have read through the cgi::session doc in detail

my code for session variables is as below

#!c:\Perl\bin\perl.exe -w use strict; use CGI::Carp qw(fatalsToBrowser); #die "Bad error here"; use CGI; use CGI::Cookie; use Data::Dumper; use CGI::Session; my $cgi = new CGI; my $session_dir = "C:\\raman"; #Generating a new session use constant SESSION_COOKIE => "MY_SITE_SIDE"; my $sid = $cgi->cookie("SESSION_COOKIE") || $cgi->param("sid") || unde +f; my $session = new CGI::Session("driver:File", $cgi, { + #giving the directory where the session var +iables will be stored Directory=>$session_dir } ) #incase of any error the session close giving an error or die $CGI::Session::errstr; my $cookie = $cgi->param(-name => SESSION_COOKIE, -value => $session->id, #sessiion expires after 3 hours -expires=>"+3h"); $cgi->header(-cookie=>$cookie); #print $cgi->header; #print $cgi->start_html; #print <<HERE; #<b>jkasgdf</b> #print "Your session id is ", $session->id(); # A cookie is being created which is then being send to the user brows +er $session->param('username', 'raman'); #$session->save_param($cgi, ["username", "password"]); #print $session->param('username'); #prints raman # this line saves all the available/accessible CGI params my $x= $session->save_param($cgi); #print $x; print "\n"; #returns the time printf("Session was created on %s\n", scalar localtime($session->ctime +)); #printf ("Session login time ", localtime( $session{_session_ctime})); #using data:;dumper to print the session variables getting stored #print Dumper(\@variable);

Replies are listed 'Best First'.
Re: tracking session:variables
by Joost (Canon) on Jun 01, 2005 at 10:33 UTC
    There's a bug in your code: $cgi->cookie("SESSION_COOKIE") should be $cgi->cookie(SESSION_COOKIE) (SESSION_COOKIE is a constant - if you quote it, you'll end up with the string "SESSION_COOKIE" instead of the value of the constant).

    As I've mentioned before, you don't need all that crufty code just to set up a session.

    In other words, you probably don't get a persistent session at all. You have been working on this script for over a week now. Have you actually checked whether it does what you think it does?

Re: tracking session:variables
by castaway (Parson) on Jun 01, 2005 at 10:26 UTC
    Get the userid from where? From the user, or from the session? Basic sessioning goes like this:

    1. A user connects to your CGI, you check if there is a sessionid parameter, if not, you create them a new session.
    2. Either you provide them a form to enter a userid in, or you assign them one, you put that data in the session (from the cgi parameter of the form, or using the userid you made up)
    3. The user visits another page, you notice they have a sessionid parameter, you use that id to create a CGI::Session object, and read the userid from that object
    So, where is the problem exactly?

    C.

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-19 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found