PDA

View Full Version : storing data in session variable


Rakish
08-25-2006, 06:41 PM
I want to store the username and login_id in the session .

i am tryin to do this.

$session = $q->cookie(-name=>$session->name,-value=>$session->id); #session cookie
$session{"username"} = $q->param("$usernameVar");
$session{"login_id"} = $q->param("$login_id");
$cookie13 = $q->cookie(-name=>'login_id',-value=>$login_id); #cookie 1
print $q->header(-location=>'show_profile.pl',-cookie=>[$cookie13,$session]);

Software error:

Global symbol "%session" requires explicit package name at /var/www/cgi-bin/catalog/testlogin.pl line 145.
Global symbol "%session" requires explicit package name at /var/www/cgi-bin/catalog/testlogin.pl line 146.
Execution of /var/www/cgi-bin/catalog/testlogin.pl aborted due to compilation errors.

I have defined the session variable as:

my $session;

if i just write

$session = $q->cookie(-name=>$session->name,-value=>$session->id); #session cookie
$cookie13 = $q->cookie(-name=>'login_id',-value=>$login_id); #cookie 1
print $q->header(-location=>'show_profile.pl',-cookie=>[$cookie13,$session]);

it works fine by just sending session cookie to the HTTP header.

Please help.

Rakesh

FishMonger
08-25-2006, 07:28 PM
$session and %session are not the same.

When you create the cookie, $session is a reference to a hash, but $session{username} is a hash element and is a completly different variable.

The error message is telling you that you didn't declare the %session hash.

You might want to look at using the CGI::Session module.
http://search.cpan.org/~markstos/CGI-Session-4.14/lib/CGI/Session.pm

FishMonger
08-25-2006, 07:43 PM
Using you current approach, you could do this:
my $session = $q->cookie(-name=>$session->name,-value=>$session->id); #session cookie

my %login = (login_id => $q->param("$login_id"),
username => $q->param("$usernameVar")
);

my $login = $q->cookie(-name=>'login_info',-value=>\%login);

print $q->header(-location=>'show_profile.pl',-cookie=>[$login, $session]);

Rakish
08-25-2006, 07:56 PM
Thanks Fish,

but I dont want the login_id to be passed as a cookie, query string or a hidden field, thats y i asked how can i pass it in a session, so that no one can edit login_id.

I want to store the login_id in the session and retreive it on other pages

FishMonger
08-25-2006, 08:22 PM
You need to use server side session management, which is what the CGI::Session module is used for

Rakish
08-28-2006, 04:49 PM
Thankyou so much Fish,
You are a life saver.

I appreciate it.

Rakesh