Re: CGI::Session with MySQL Driver



Alexandre Jaquet wrote:

> Mark Clements a écrit :
> > Alexandre Jaquet wrote:
> >
> >
> > > Mark Clements a écrit :
> > >
> > >
> > > > Show us your code using param.
> > > >
> > > > Does it work with the File driver?
> > > >
> > > > Mark
> > >
> > > sub login {
> >> my $username = param('user_name');
> >> my $userpassword = param('user_password');
> > >
> >> my ($user_name,$user_password)=sqlSelect("nom_utilisateur ,
> > > mot_de_passe", "personne", "nom_utilisateur = '$username' AND
> > > mot_de_passe='$userpassword'"); my $dir =
> > > "C:/indigoperl/apache/htdocs/recordz/"; if ($user_name &&
> > > $user_password) { $CGI::Session::MySQL::TABLE_NAME = 'session';
> > > my $SESSION = new CGI::Session( "driver:MySQL", undef, { Handle =>
> > > $dbh} ); @my_array = ("$username");
> > > $SESSION->param("user_name", \@my_array);
> > > open (FILE, "<$dir/myaccount.html") or die "cannot open file
> > > $dir/myaccount.html"; print "Content-type: text/html\n\n";
> > > $CGISESSID = $SESSION->id();
> > > while (<FILE>) {
> > > s/\$LABEL{'([\w]+)'}/$SERVER{$1}/g;
> > > s/\$LANG/$lang/g;
> > > s/\$ERROR{'([\w]+)'}//g;
> > > s/\$VINYL{'news'}/$string/g;
> > > s/\$SESSIONID/$CGISESSID/g;
> > > s/\$VINYL{'search'}//g;
> > > print $_;
> > > }
> > > close (FILE);
> > >
> >> }
> >> else {
> > > #}}
> >
> >
> > You need to learn to partition your problem. There is a load of gumpf
> > here that gets in the way of the issue that you are having and makes it
> > more difficult for both you and us to find a solution. You've indicated
> > this works with the File driver, but you haven't shown us the error
> > message or unexpected behaviour you get with the code above.
> >
> > Remember to run with strict turned on - I don't think you have with the
> > code above. Break down the code into the smallest number of lines that
> > exhibit the behaviour you are trying to demonstrate (you have been
> > asked to do this on other occasions). I'm not going to write the test
> > script for you myself.
> >
> > Mark
> >
> >
> >
>
>
> Here a complete test script :
>
> #!perl -w
> use CGI qw(:standard);
> use Switch;
> use CGI::Session qw/-ip-match/;
> use vars qw($dbh $session);
> use DBI;
> use strict;
>
> my $query ;
>
> $dbh ||= sqlConnect("DBI:mysql:recordz:localhost", "alexj", "xxx");
>
> execute ();
>
>
> sub execute {
> $query = new CGI ;
> my $action = $query->param('action');
> if ($action) {
> switch ($action) {
> case "login" {
> login();
> };
> case "test" {
> };
> }
> }
> }
>
> sub login {
> $session = new CGI::Session( "driver:MySQL", undef, { Handle =>
> $dbh} );
> my $username = "alexj";
> $session->param("user_name", $username);
> my $id = $session->id;
> print "Content-type: text/html\n\n";
> print "ID : $id";
> }
>
> sub sqlConnect {
> my $dbname = shift || '';
> my $dbusername = shift || '';
> my $dbpassword = shift || '';
>
> $dbh = DBI->connect($dbname, $dbusername, $dbpassword);
> if (!$dbh) {
> }
> kill 9, $$ unless $dbh;
> }

OK - this is better but you still don't quite get it. I'm not exactly sure what
your problem is at the moment (you haven't stated it clearly, despite repeated
prompting): is it that CGI::Session fails with an error message, or that you don't
get anything in the database table? To save time, and my hair, we need to see
something like:

use strict;
use warnings;

use CGI::Session qw/-ip-match/;
use DBI;

my $dbname = "dbname";
my $dbusername = "dbusername";
my $dbpassword = "dbpassword";

my $dbh = DBI->connect($dbname, $dbusername, $dbpassword)
or die "could not open db connection - error = $DBI::errstr";

my $session =
CGI::Session->new( "driver:MySQL", undef, { Handle => $dbh} );

$session->param( testkey => "testdata" );

print "stored session with id " . $session->id();

You can then check in the db table directly to see if the session has been stored.
I will leave it as an exercise to you to come up with a minimal script to pull
sessions out again.

Once the above is working properly, you can experiment with more complex data
structures and wiring it back into your cgi script.


>sub sqlConnect {
> my $dbname = shift || '';
> my $dbusername = shift || '';
> my $dbpassword = shift || '';
>
> $dbh = DBI->connect($dbname, $dbusername, $dbpassword);
> if (!$dbh) {
> }
> kill 9, $$ unless $dbh;
>}

kill? bit harsh, plus you have no idea what the error is. You want

die $DBI::errstr unless $dbh;
.



Relevant Pages

  • Re: crontab for executing perl script
    ... Alexandre Jaquet wrote: ... > I can't found why my script doesn't run: ... > crontab: ... This will give you the *reason* openfailed, ...
    (comp.lang.perl.misc)
  • Re: mod_perl error
    ... Alexandre Jaquet wrote: ... Your script doesn't contain anything mod_perl specific. ... Possibly you have conflicting mod_perl modules on your system. ... Mark ...
    (comp.lang.perl.misc)
  • Re: Perl script runs from command prompt but not from Task Scheduler in Win2000
    ... Mark Clements wrote: ... >> I have a script that runs fine from the command prompt, but that fails ... Or put a line like the following at the start of the script: ...
    (comp.lang.perl.misc)