Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing a permanent cookie using CGI::Session 1

Status
Not open for further replies.

Leland123

Programmer
Aug 15, 2001
402
US
I have a shopping cart application that uses CGI::Session. When a visitor logs into the system, a cookie is written to the visitor's computer. The cookie contains the customer's account number.

I want to make the cookie permanent on the visitor's computer, so after a customer logs into the shopping cart for the first time, he/she will not be required to log into the system again, as CGI::Session will read the visitor's cookie to capture the account number from the cookie on the customer's computer.

Presently, the cookie seem to be working with the current browser session, as the customer move from web page to web page. The customer can even return to the start page, which is a kind of splash screen, and then come back into the shopping cart, as long as the browser is never closed. Once the browser is closed and the visitor begins a new session by re-starting his browser, the cookie seems to have been deleted or to have disappeared.

I have tried passing 0 (eg zero) and 10y (eg 10 years) to the CGI::Session module, which works until the browser is closed and started again.

Does anyone know what is required to set a permanent cookie on a customer's computer using CGI::Session.

Regards,

Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
In my main class or module (eg Smglobal.pm) I have a parameter set as follows:

$self->{SESSION_EXPIRE} = "+10y";

In my Sm_session class or module I have the following method which Smglobal.pm inherites:

######################################################
## write_session_cookie
######################################################
sub write_session_cookie
{
my $self = shift;

my ( $su_logged, $customer_no, $http_referer, $query_string ) = @_;

use CGI::Session;

use CGI;

my $cgi = new CGI;

CGI::Session->name("SM_SID");

$session = new CGI::Session( undef, $cgi, { Directory=>$self->{ COOKIE_DIR } } );

$session->expire('$self->{SESSION_EXPIRE}');

$session->param( "su_logged" , $su_logged );
$session->param( "customer_no" , $customer_no );
$session->param( "http_referer" , $http_referer );
$session->param( "query_string" , $query_string );

$session->save_param($cgi);

$cookie = $cgi->cookie(SM_SID => $session->id );

print $cgi->header( -cookie => $cookie );

return;

I create a reference to the Smglobal class or modules and then call the write_session_cookie method or sub_routine as follows:

#--------------------------

$oMy = Smglobal->new();

$oMy->retrieve_session_cookie();

if ( $oMy->{CUSTOMER_NO} > 0 )
{

print qq~Content-type: text/html\n\n~;
$FORM{ clientno } = $oMy->{ CUSTOMER_NO };

my $debug_cookie = "false";
#my $debug_cookie = "true";
if ( $debug_cookie eq "true" )
{
print qq
~
SU logged is $oMy->{SU_LOGGED}<br>
Customerno is $oMy->{CUSTOMER_NO}<br>
Http Referer is $oMy->{HTTP_REFERER}<br>
Affiliate No is $oMy->{AFFILIATE_NO}<br>
~;
}
}
else
{
$oMy->write_session_cookie( "f", -123456789, $ENV{HTTP_REFERER}, $ENV{QUERY_STRING} );
}

After the cookie is written, I can go to my Firefox browser and look at the cookie under Tool --> Options --> Privacy --> View Cookies. I find the cookie under:

smvfp.com
|
------- SM_SID

When I click on the SM_SID cookie I see:

Name: SM_SID
Content: c7409394c5035c26a1ba7f4fcf566bdd
Path: /
Send For: Any type of connection
Expires: at end of session

No matter what I pass to $session->expire('$self->{SESSION_EXPIRE}') of my "sub write_session_cookie" method or class, I alway get a cookie that expires at the end of the session. I have look at other cookies in Firefox, and they alway show a time/date when the cookie will expire.

I hope this helps.


Regards,

LelandJ


Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
This looks suspicious:

Code:
$session->expire('$self->{SESSION_EXPIRE}');

you have $self->{SESSION_EXPIRE} inside of single-quotes which is probably killing the variable interpolation. Remove the quotes and retry your script.
 
Thanks for the hint KevinADC. I've already eliminated the syntax as a possible cause by entering the expriation info directly into the line that is passing the variable like:

$session->expire('+10y');

This essentially comes right out of the CPAN doc.

I've change the overall syntax of the methods several times, based on whichever cpan, or other doc, I happen to be reading, but no joy. I may have to take a look at the actual CGI::Sessions.pm file to see what is going on.

I've crashed my Linux desktop, so I will need to reboot. I may wait until tomarrow to take this on again.

Regards,

LelandJ

Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
isn't $session->expire('+10y'); to expire the session? Try printing the value of $cookie to the screen so you can see just what it's value is:

Code:
$cookie = $cgi->cookie(SM_SID => $session->id );
print $cgi->header( -cookie => $cookie );
\$cookie = $cookie

then we can take it from there.
 
As long as you're printing the cookie headers through CGI, just add -expires to the cookie call.

Change this:
Code:
      $cookie = $cgi->cookie(SM_SID => $session->id );

to this:
Code:
$cookie = $cgi->cookie (
   -name => 'SM_SID',
   -value => $session->id,
   -expires => '+10y',
);

There might be a way to incorporate the expires in without going to that form of hash, so it would look closer to how the original line was without the -name -value stuff, but you'd have to look at the docs for that, but the code I posted I know is correct. Give that a try. :)
 
Thanks Kirsle, your code works. In case anybody else is interested, here is the code that works:

######################################################
## write_session_cookie
######################################################
sub write_session_cookie
{
my $self = shift;

my ( $su_logged, $customer_no, $http_referer, $query_string ) = @_;

use CGI;
use CGI::Session;
CGI::Session->name("SM_SID");


my $cgi = new CGI;
my $session = new CGI::Session(undef, $cgi, {Directory=>$self->{ COOKIE_DIR} } );

$cookie = $cgi->cookie (
-name => 'SM_SID',
-value => $session->id,
-expires => '+10y',
);

print $cgi->header(-cookie=>$cookie);

$session->param( "su_logged" , $su_logged );
$session->param( "customer_no" , $customer_no );
$session->param( "http_referer" , $http_referer );
$session->param( "query_string" , $query_string );

$session->save_param($session);



return;

}

#-----------------------------------------

Regards,

LelandJ

Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top