Gossamer Forum
Home : General : Perl Programming :

Apache::Session

Quote Reply
Apache::Session
Hi,

I'm new to this forum so forgive my noobquestions ;)

I took this code from CPAN:
Code:
use strict;

use DBI;

use Apache::Session::MySQL;



my %session;



#make a fresh session for a first-time visitor

tie %session, 'Apache::Session::MySQL';



#stick some stuff in it

$session{visa_number} = "1234 5678 9876 5432";



#get the session id for later use

my $id = $session{_session_id};



#...time passes...



#get the session data back out again during some other request

my %session;

tie %session, 'Apache::Session::MySQL', $id;



print($session{visa_number});



#delete a session from the object store permanently

tied(%session)->delete;

I want to use this to see which people logged in to my site.
But when I run this I get this error:

Code:
Content-type:text/html

Can't connect( HASH(0x19ec484)), no database driver specified and DBI_DSN env
var not set at C:/Perl/lib/Apache/Session/Lock/MySQL.pm line 36

How can I fix this?
Quote Reply
Re: [Sensei_D] Apache::Session In reply to
Hmm no one knows Unsure
Then does anyone know where I can find some sort of tutorial on how to create some kind of login system in Perl?
Quote Reply
Re: [Sensei_D] Apache::Session In reply to
That's something wrong with your MySQL setup/driver not Apache::Session. I've used Apache::Session (Postgres) with CGI::Application/mod_perl successfuly in the past, and it works like a dream.

- wil
Quote Reply
Re: [Sensei_D] Apache::Session In reply to
Have you read over the POD yet? It looks as if you're missing most of the connection parameters.

http://search.cpan.org/...on/MySQL.pm#SYNOPSIS

~Charlie
Quote Reply
Re: [Chaz] Apache::Session In reply to
Yes I used the handles and the drivers should be OK.
Maybe someone can provide me with an examplefile which already works so I can test my configuration here with it...
Quote Reply
Re: [Sensei_D] Apache::Session In reply to
The code you posted above won't work out of the box. Try the link I posted above. It has info specific to the MySQL driver for Apache::Session. From the POD:

Code:
use Apache::Session::MySQL;

#if you want Apache::Session to open new DB handles:

tie %hash, 'Apache::Session::MySQL', $id, {
DataSource => 'dbi:mysql:sessions',
UserName => $db_user,
Password => $db_pass,
LockDataSource => 'dbi:mysql:sessions',
LockUserName => $db_user,
LockPassword => $db_pass
};

#or, if your handles are already opened:

tie %hash, 'Apache::Session::MySQL', $id, {
Handle => $dbh,
LockHandle => $dbh
};

~Charlie
Quote Reply
Re: [Chaz] Apache::Session In reply to
Yes I tried your suggestion chaz Wink
But I still get exactly the same error.
Quote Reply
Re: [Sensei_D] Apache::Session In reply to
I have got this now, but it generates the same error Frown
Code:
#!C:\Perl\bin\Perl.exe
print "Content-type:text/html\n\n";

use strict;
use DBI;
use Apache::Session::MySQL;

my $db="Sensei-d";
my $host="localhost";
my $user="****";
my $password="****";
my $Database = DBI->connect ("DBI:mysql:$db:$host",
$user,
$password)
or die "Can't connect to database: $DBI::errstr<br>";

#make a fresh session for a first-time visitor
my %session;
tie %session, 'Apache::Session::MySQL', undef, {
Handle => $Database,
LockHandle => $Database
};

#stick some stuff in it
$session{visa_number} = "1234 5678 9876 5432";

#get the session id for later use
my $id = $session{_session_id};

#...time passes...

#get the session data back out again during some other request
my %session;
tie %session, 'Apache::Session::MySQL', $id;

print($session{visa_number});

#delete a session from the object store permanently
tied(%session)->delete;
Quote Reply
Re: [Sensei_D] Apache::Session In reply to
Code:
#...time passes...

#get the session data back out again during some other request
my %session;
tie %session, 'Apache::Session::MySQL', $id;

I think you still need to pass in either the info to make the database connection or a DBI handle object to make the connection here. I use CGI::Session instead of Apache::Session so I'm not 100% sure but looking though the POD it sure looks like you need it.

~Charlie
Quote Reply
Re: [Chaz] Apache::Session In reply to
Ohw damn I forgot that one Blush Thnx!
Ok the error is gone now and the session can be made. But I can't get it later on... The code after "#time passes" doesn't work because visa number value inside the session becomes empty after tie-ing it again with the ID from before.

I thought it would work nowUnsure