Home > Archive > PERL Miscellaneous > May 2005 > CGI::Session with MySQL Driver
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
CGI::Session with MySQL Driver
|
|
| Alexandre Jaquet 2005-05-30, 8:57 am |
| Does anyone 've a sample test script I can use.
I've search the web but didn't find something relevant.
cheers :)
| |
| Mark Clements 2005-05-30, 8:57 am |
| Alexandre Jaquet wrote:
> Does anyone 've a sample test script I can use.
>
> I've search the web but didn't find something relevant.
>
> cheers :)
er - how about
t/mysql.t
from the CGI::Session distribution itself.
Mark
| |
| Fabian Pilkowski 2005-05-30, 8:57 am |
| Subject: CGI::Session with MySQL Driver
* Alexandre Jaquet schrieb:
> Does anyone 've a sample test script I can use.
>
> I've search the web but didn't find something relevant.
Take the one I recently fixed for you in your last thread (look at
<news:3fvvicF9s5ovU1@individual.net> if you don't remember) as basis.
Change the call of CGI::Session's constructor from
my $session = new CGI::Session( "driver:File", undef,
{Directory => 'C:/sessions'} );
to
my $dbh = DBI->connect( ... ) # create databasehandle
my $session = new CGI::Session( "driver:MySQL", undef,
{ Handle => $dbh} );
Pay attention to the second parameter which should be remain as it
already is in the various situations. Then read the docs to learn how
creating the necessary table in your MySQL database, at
http://search.cpan.org/~sherzodr/CG...ession/MySQL.pm
That's all. If you have a specific problem, try to check the docs first
-- perhaps there is already mentioned a solution.
regards,
fabian
| |
| Alexandre Jaquet 2005-05-30, 8:57 am |
| Fabian Pilkowski a écrit :
> Subject: CGI::Session with MySQL Driver
>
> * Alexandre Jaquet schrieb:
>
>
>
>
> Take the one I recently fixed for you in your last thread (look at
> <news:3fvvicF9s5ovU1@individual.net> if you don't remember) as basis.
> Change the call of CGI::Session's constructor from
>
> my $session = new CGI::Session( "driver:File", undef,
> {Directory => 'C:/sessions'} );
>
> to
>
> my $dbh = DBI->connect( ... ) # create databasehandle
> my $session = new CGI::Session( "driver:MySQL", undef,
> { Handle => $dbh} );
>
> Pay attention to the second parameter which should be remain as it
> already is in the various situations. Then read the docs to learn how
> creating the necessary table in your MySQL database, at
>
> http://search.cpan.org/~sherzodr/CG...ession/MySQL.pm
>
> That's all. If you have a specific problem, try to check the docs first
> -- perhaps there is already mentioned a solution.
>
> regards,
> fabian
I've read this doc, but it's doesn't seem to have example with new
defined field
I want to store user_name in session table
$dbh is previously initialized
$CGI::Session::MySQL::TABLE_NAME = 'session';
my $SESSION = new CGI::Session("driver:MySQL",$query, $dbh);
$SESSION->store($dbh,$id,'',"user_name => $username");
error message :
Can't connect to data source , no database driver specified and DBI_DSN
env var not set at C:/indigoperl/perl/site/lib/CGI/Session/MySQL.pm line
Can't get DBI::db=HASH(0x1f21af0)->{Handle}: unrecognised attribute at
C:/indigoperl/perl/site/lib/CGI/Session/MySQL.pm line 131
| |
| Mark Clements 2005-05-30, 3:57 pm |
| Alexandre Jaquet wrote:
> Fabian Pilkowski a écrit :
<snip>[color=darkred]
[color=darkred]
>
> I've read this doc, but it's doesn't seem to have example with new
> defined field
>
> I want to store user_name in session table
>
> $dbh is previously initialized
>
> $CGI::Session::MySQL::TABLE_NAME = 'session';
> my $SESSION = new CGI::Session("driver:MySQL",$query, $dbh);
> $SESSION->store($dbh,$id,'',"user_name => $username");
>
> error message :
>
> Can't connect to data source , no database driver specified and
> DBI_DSN env var not set at
> C:/indigoperl/perl/site/lib/CGI/Session/MySQL.pm line
>
> Can't get DBI::db=HASH(0x1f21af0)->{Handle}: unrecognised attribute
> at C:/indigoperl/perl/site/lib/CGI/Session/MySQL.pm line 131
Why are you calling store? This is not mentioned in the documentation
and should be considered an internal method, ie don't call it yourself.
What does
$dbh->ping()
return?
Is your script failing at new or at store?
Until you learn to ask yourself this sort of question you aren't going
to make much progress.
Mark
| |
|
|
| Alexandre Jaquet 2005-05-30, 3:57 pm |
| A. Sinan Unur a écrit :
> Alexandre Jaquet <alexj@freesurf.ch> wrote in news:429af5eb$0$1145$5402220f@news.sunrise.ch:
>
>
>
>
> <URL: http://search.cpan.org/~sherzodr/CG...ING_NEW_SESSION>
>
>
nothing about storing data in database ...
$SESSION = new CGI::Session( "driver:MySQL", undef, { Handle =>$dbh} );
$SESSION->store($dbh,$SESSION->id,'',"user_name => $username");
store now return : Can't use string ("b7f26a2095fb2ff4e11e8ae1295d767e")
as an ARRAY ref while "strict refs" in use at ...
the string b7f26a2095fb2ff4e11e8ae1295d767e represent the sessionid
what's wrong ?
| |
| Mark Clements 2005-05-30, 3:57 pm |
| Alexandre Jaquet wrote:
> A. Sinan Unur a écrit :
>
> nothing about storing data in database ...
>
> $SESSION = new CGI::Session( "driver:MySQL", undef, { Handle =>$dbh}
> ); $SESSION->store($dbh,$SESSION->id,'',"user_name => $username");
>
> store now return : Can't use string
> ("b7f26a2095fb2ff4e11e8ae1295d767e") as an ARRAY ref while "strict
> refs" in use at ...
>
> the string b7f26a2095fb2ff4e11e8ae1295d767e represent the sessionid
> what's wrong ?
You really don't read the documentation do you?
From the tutorial that Sinan pointed you at:
@my_array = ("apple", "grapes", "melon", "casaba");
$session->param("fruits", \@my_array);
Don't use store, use param.
Don't make us read the docs for you.
Mark
| |
| Alexandre Jaquet 2005-05-30, 3:57 pm |
| Mark Clements a écrit :
> Alexandre Jaquet wrote:
>
>
>
>
> You really don't read the documentation do you?
>
> From the tutorial that Sinan pointed you at:
>
> @my_array = ("apple", "grapes", "melon", "casaba");
> $session->param("fruits", \@my_array);
>
> Don't use store, use param.
>
> Don't make us read the docs for you.
>
> Mark
That's what I've try first it's doesn't store value key pair user_name
$username ...
| |
| Mark Clements 2005-05-30, 3:57 pm |
| Alexandre Jaquet wrote:
> Mark Clements a écrit :
>
> That's what I've try first it's doesn't store value key pair
> user_name $username ...
Show us your code using param.
Does it work with the File driver?
Mark
| |
| Alexandre Jaquet 2005-05-30, 3:57 pm |
| Mark Clements a écrit :
> Alexandre Jaquet wrote:
>
>
>
>
> 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 {
#}}
| |
| Alexandre Jaquet 2005-05-30, 3:57 pm |
| Mark Clements a écrit :
> Alexandre Jaquet wrote:
>
>
>
>
> Show us your code using param.
>
> Does it work with the File driver?
>
> Mark
With filedriver that's work
| |
| A. Sinan Unur 2005-05-30, 3:57 pm |
| Alexandre Jaquet <alexj@freesurf.ch> wrote in
news:429b00f5$0$1156$5402220f@news.sunrise.ch:
> A. Sinan Unur a écrit :
3.95/Session/Tutorial.pm#[color=darkred]
> nothing about storing data in database ...
How would you know? You did not read any of the documentation.
> $SESSION = new CGI::Session( "driver:MySQL", undef, { Handle =>$dbh}
> ); $SESSION->store($dbh,$SESSION->id,'',"user_name =>
> $username");
I cannot fathom where you got the idea that you should call the store
method on a CGI::Session object. There is no such method in the public
interface or documentation or even the source code of CGI::Session.
Now, CGI::Session::Driver::mysql does implement a store method, as do
all the drivers. But, that is not part of the public interface of a
CGI::Session::Driver::DBI. In fact, you are not supposed to manipulate
CGI::Session::Driver::DBI objects by yourself at all.
> what's wrong ?
You did not read the documentation.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Mark Clements 2005-05-30, 3:57 pm |
| Alexandre Jaquet wrote:
> Mark Clements a écrit :
>
>
> 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
| |
| Alexandre Jaquet 2005-05-30, 3:57 pm |
| Mark Clements a écrit :
> Alexandre Jaquet wrote:
>
>
>
>
> 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;
}
| |
| Fabian Pilkowski 2005-05-30, 3:57 pm |
| * Alexandre Jaquet schrieb:
> Mark Clements a écrit :
[color=darkred]
>
> sub login {
> my $username = param('user_name');
> my $userpassword = param('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);
This creates a new session since the second param to new() is undef. The
param()-call should set the value for "user_name" in *this* session. It
seems to be all right.
How do you verify that this value is not saved in your session? Do you
look into the database by hand, don't you?
I assume your problem is to get back this session instead of creating a
new one each time your script is called. Could this be?
regards,
fabian
| |
| Alexandre Jaquet 2005-05-30, 3:57 pm |
| Fabian Pilkowski a écrit :
> * Alexandre Jaquet schrieb:
>
>
>
>
>
>
> [...]
>
>
>
>
> This creates a new session since the second param to new() is undef. The
> param()-call should set the value for "user_name" in *this* session. It
> seems to be all right.
>
> How do you verify that this value is not saved in your session? Do you
> look into the database by hand, don't you?
>
> I assume your problem is to get back this session instead of creating a
> new one each time your script is called. Could this be?
>
> regards,
> fabian
hi fabian, yes I look it by doing a sqlquery to verify, but the field
user_name is alway empty
first I want to make my datas persistent into the db then I will check
out if exising session can be reopended
| |
| Mark Clements 2005-05-30, 3:57 pm |
| Alexandre Jaquet wrote:
> Mark Clements a écrit :
>
>
> 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;
| |
| Alexandre Jaquet 2005-05-30, 3:57 pm |
| Mark Clements a écrit :
> Alexandre Jaquet wrote:
>
>
>
>
> 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.
>
>
>
>
>
> kill? bit harsh, plus you have no idea what the error is. You want
>
> die $DBI::errstr unless $dbh;
thanks, I think there is a problem under windows, $session->param(
user_name => "testdata" ); is not written for a session table
based on a db :
CREATE TABLE `sessions` (
`id` char(32) NOT NULL,
`a_session` text NOT NULL,
`user_name` varchar(100) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
| |
| A. Sinan Unur 2005-05-30, 3:57 pm |
| Alexandre Jaquet <alexj@freesurf.ch> wrote in
news:429b107e$0$1147$5402220f@news.sunrise.ch:
> thanks, I think there is a problem under windows, $session->param(
> user_name => "testdata" ); is not written for a session table
>
> based on a db :
>
> CREATE TABLE `sessions` (
> `id` char(32) NOT NULL,
> `a_session` text NOT NULL,
> `user_name` varchar(100) NOT NULL,
> UNIQUE KEY `id` (`id`)
> ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
The session data are stored in the a_session field by the CGI::Session
driver. You should use the param method of the CGI::Session object you
have created to access that information.
Trust me, this is in the documentation.
Now, another problem with your code was pointed out elsethread
(regarding the arguments to the CGI::Session constructor). You will need
to fix that before doing anything else.
READ THE DOCUMENTATION.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Fabian Pilkowski 2005-05-30, 3:57 pm |
| * Alexandre Jaquet schrieb:
> Mark Clements a écrit :
[problems with CGI::Session]
>
> Here a complete test script :
Ok, I have already suggested to use your script with my fixes as basis.
This is your script, but without my fixes!?
>
> #!perl -w
> use CGI qw(:standard);
> use Switch;
> use CGI::Session qw/-ip-match/;
> use vars qw($dbh $session);
Please, don't use "use vars". Declare your vars with my() instead.
> use DBI;
> use strict;
>
> my $query ;
Please, assign "new CGI" to this var.
>
> $dbh ||= sqlConnect("DBI:mysql:recordz:localhost", "alexj", "xxx");
Always, $dbh isn't defined here since its the first appearance of it.
my $dbh = sqlConnect("DBI:mysql:recordz:localhost", "alexj", "xxx");
>
> execute ();
>
> sub execute {
> $query = new CGI ;
Please, delete this line!
> 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";
Please, send the HTTP header you really want instead of making your own.
Try out:
print $session->header();
> 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;
> }
I thought, sqlConnect should return a valid databasehandle. This does
not. Try to remove the last three lines of this sub.
Additionally, I rewrite your testscript with all fixes I mentioned
above. It seems you won't put them in place if I just write it.
#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw( fatalsToBrowser );
use Switch;
use CGI::Session qw( -ip-match );
use DBI;
my $dbh = DBI->connect( "DBI:mysql:recordz:localhost", "alexj", "xxx" );
my $query = new CGI;
my $action = $query->param( 'action' );
if ( $action ) {
switch ( $action ) {
case "login" {
my $session = new CGI::Session( "driver:MySQL", undef, {Handle=>$dbh} );
$session->param( "user_name", "alexj" );
print $session->header();
print "SESSIONID: ", $session->id;
};
case "test" {
my $session = new CGI::Session( "driver:MySQL", $query, {Handle=>$dbh} );
print $session->header();
print "SESSIONID: ", $session->id;
print "USERNAME: ", $session->param( "user_name" );
};
}
}
else {
print CGI::header(), "no action param given";
}
__END__
Place this script on your webserver and try to run it. Once with param
"action=login" and afterwards with "action=test". For me, this works as
it should.
regards,
fabian
| |
| Alexandre Jaquet 2005-05-30, 3:57 pm |
| Fabian Pilkowski a écrit :
> * Alexandre Jaquet schrieb:
>
>
>
> [problems with CGI::Session]
>
>
>
>
> Ok, I have already suggested to use your script with my fixes as basis.
> This is your script, but without my fixes!?
>
>
>
>
> Please, don't use "use vars". Declare your vars with my() instead.
>
>
>
>
> Please, assign "new CGI" to this var.
>
>
>
>
> Always, $dbh isn't defined here since its the first appearance of it.
>
> my $dbh = sqlConnect("DBI:mysql:recordz:localhost", "alexj", "xxx");
>
>
>
>
> Please, delete this line!
>
>
>
>
> Please, send the HTTP header you really want instead of making your own.
> Try out:
>
> print $session->header();
>
>
>
>
> I thought, sqlConnect should return a valid databasehandle. This does
> not. Try to remove the last three lines of this sub.
>
> Additionally, I rewrite your testscript with all fixes I mentioned
> above. It seems you won't put them in place if I just write it.
>
>
> #!/usr/bin/perl -w
> use strict;
> use CGI;
> use CGI::Carp qw( fatalsToBrowser );
> use Switch;
> use CGI::Session qw( -ip-match );
> use DBI;
>
> my $dbh = DBI->connect( "DBI:mysql:recordz:localhost", "alexj", "xxx" );
> my $query = new CGI;
>
> my $action = $query->param( 'action' );
> if ( $action ) {
> switch ( $action ) {
> case "login" {
>
> my $session = new CGI::Session( "driver:MySQL", undef, {Handle=>$dbh} );
> $session->param( "user_name", "alexj" );
> print $session->header();
> print "SESSIONID: ", $session->id;
>
> };
> case "test" {
>
> my $session = new CGI::Session( "driver:MySQL", $query, {Handle=>$dbh} );
> print $session->header();
> print "SESSIONID: ", $session->id;
> print "USERNAME: ", $session->param( "user_name" );
>
> };
> }
> }
> else {
> print CGI::header(), "no action param given";
> }
> __END__
>
>
> Place this script on your webserver and try to run it. Once with param
> "action=login" and afterwards with "action=test". For me, this works as
> it should.
>
> regards,
> fabian
Great many thanks fabian :)
| |
| Fabian Pilkowski 2005-05-30, 3:57 pm |
| * Alexandre Jaquet schrieb:
> Fabian Pilkowski a écrit :
>
> hi fabian, yes I look it by doing a sqlquery to verify, but the field
> user_name is alway empty
Ok, as Sinan has already written, this is your real problem. The field
user_name you have in your MySQL table will never be filled. It'll be
empty forever (and that's ok).
Please look into the field "a_session" where a dump of your session is
stored (in Data::Dumper-style as default, but you know: this is already
mentioned in the docs).
>
> first I want to make my datas persistent into the db then I will check
> out if exising session can be reopended
Please, read the docs first to learn where you have to look for your
values instead of looking anywhere. Your bad assumption is, you need
extra fields in your table -- which isn't true. Well, I know you have
read this often in this thread, but:
Read the documentaion first.
Btw, you never said that you want to store your values in separate
fields in your MySQL table. Nobody could know about this, because it's
not the usual way in combination with CGI::Session.
regards,
fabian
| |
| Tad McClellan 2005-05-30, 3:57 pm |
| Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de> wrote:
> * Alexandre Jaquet schrieb:
>
> Please, don't use "use vars". Declare your vars with my() instead.
Let's make that more accurate:
Please, don't use package variables when lexical variables will do.
Declare your vars with my() instead.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
|
|
|