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

tracking session:variables

by rjsaulakh (Beadle)
on Jun 01, 2005 at 09:55 UTC ( #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?


      i have some more query before i get back on it.
      1. i want this of my progarm to be a perl program so that it should only excute and not create or display a new page <
      and at the same time session variables should be stored in the directory

      i would like u to first guide me on this please

        I can't read your mind, or do your work for you, neither will I.

        The point of a perl CGI program is to create web pages for the users, using perl. Executing the perl in the CGI produces a webpage, usually. There is no point in using it if that is not what you want to do.

        In order to help you at all, I think you will need to try and describe, in words, and *at length* what exactly you are trying to attempt. Not just "save session variables", but *what* session variables? What are they for? What is the CGI going to do when it is finished?

        If you are having problems because you dont know enough english, then please try and find someone who does, put your location on your homenode, and look at other peoples to see where they are from, and find someone that speaks your language.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://462377]
Approved by castaway
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-06-20 02:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.