Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Thinking I have narrowed down the issue, here is a better question.

My script, 'index', opens an existing session - because a session does exist from when it was created by a login script.

It does correctly use values from that session in the page output so evidently, it's accessing the session from either memory or the server's sessions_storage dir.

The script is written so as to add two values to the session but, that's not actually happening. And this is where it gets annoyingly frustrating.

After running the script, I check the session file in filezilla. Those two values do not exist. However, if I output a dump of the session, at the bottom of my script, the two values show in that output.

If I delete the session from my browser and then reload the page, those two values and a few others are showing in the new session file but, of course, the other values stored from previous files (eg login) are missing.

This I have worked out thus far:-

  1. All other files (from login thru to this 'index') are creating and/or storing and retrieving to/from the session without issue.
  2. 'Index' script is not adding to the existing session file.
  3. New session forced by deleting session cookie from browser shows the data is being stored as expected in the correct server dir.
  4. Using flush(); at the end of my script (or anywhere after session creation/loading); has made no difference.

Can any of you with fresh eyes tell me what's (not) going on?

my $sessions_dir_location = '/' . $var . '/' . $www . '/' . $vhosts . '/' . $domain . '/name_of_sessions_storage_dir/';

my $session = new CGI::Session(undef, $cgi, {Directory=>"$sessions_dir_location"}) or die CGI::Session->errstr;


my $session_id = $session->id();
$session->flush();
my %vars = $cgi-Vars;
my $business_id = $vars{'business_id'};

print qq(<pre>bid=$business_id</pre>); #successful

$session->param('business_id', $business_id); #unsuccessful

print qq(<pre>session_id = $session_id); #successful

print $session->dump; # shows the business_id value as being stored. 
print qq(</pre>);
question from:https://stackoverflow.com/questions/65850001/session-data-not-being-updated-by-script-using-that-session

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
761 views
Welcome To Ask or Share your Answers For Others

1 Answer

The following works for me, it increases the session parameter business_id by one for each call:

use strict;
use warnings;
use CGI;
use CGI::Session;

my $cgi = CGI->new();
my $sessions_dir_location = "/tmp/sessions";

# Data Source Name, defaults to "driver:file;serializer:default;id:md5"
my $dsn = undef;

# new() : returns new session object, or undef on failure. Error message is
#   accessible through errstr() - class method.
my $session = CGI::Session->new(
    $dsn, $cgi, {Directory=>"$sessions_dir_location"}) or die CGI::Session->errstr();
my %vars = $cgi->Vars;
my $cgi_bsid = $vars{business_id};
my $session_bsid = $session->param("business_id");
my $new_bsid = $cgi_bsid // $session_bsid // 0;
$new_bsid++;
$session->param('business_id', $new_bsid);
$session->flush() or die CGI::Session->errstr();

# CGI::Session will use this cookie to identify the user at his/her next request
#   and will be able to load his/her previously stored session data.
print $session->header();

my $session_id = $session->id();
print $cgi->start_html();
print join "<br>",
  qq(session_id=$session_id),
  qq(cgi_bsid=$cgi_bsid),
  qq(session_bsid=$session_bsid),
  qq(new_id=$new_bsid);
print $cgi->end_html();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...