Make database handle persist in CGI/DBI application?
Make database handle persist in CGI/DBI application?
am 23.02.2006 18:15:53 von mfanderson
Hi all,
I am writing a Web app in perl/cgi/dbi which (1) attempts to verify
logins by checking against database user passwords, and (2) follows good
dbi practices by logging the user in once at the beginning of the session
instead of logging in immediately prior to accessing the database.
I am running afoul of CGI parameter passing conventions. I do( and
find) the following
use CGI "standard";
use DBI;
my $dbh;
if ($condition){
$dbh = $DBI:connect($connectionString, $login, $password);
# find connection is successful at this poing
sub fubar{
my $sth=$dbh->prepare($cmd);
# find that $dbh is undefined.
I have tried passing the handle in as a ref parameter, using $dbhRef = /*dbh
(maybe this one was done incorrectly) and tried passing a $dbhRef as a
value on a hidden textfield. None of these work. I tend to get
"not an array" when I try to do $$dbhref to dereference the handle.
Is there any way to make the handle persist?
Would I have this trouble if I used Mason?
Thanks in advance,
Mary Anderson
Re: Make database handle persist in CGI/DBI application?
am 23.02.2006 18:17:13 von Tyler
Mary Anderson wrote:
> I have tried passing the handle in as a ref parameter, using $dbhRef = /*dbh
> (maybe this one was done incorrectly) and tried passing a $dbhRef as a
> value on a hidden textfield. None of these work. I tend to get
> "not an array" when I try to do $$dbhref to dereference the handle.
>
> Is there any way to make the handle persist?
> Would I have this trouble if I used Mason?
Mary,
You need mod_perl;
http://perl.apache.org/
Check here first;
http://perl.apache.org/docs/2.0/user/coding/coding.html#C_Mo dPerl__Registry__Handlers_Family
Cheers,
Tyler
Re: Make database handle persist in CGI/DBI application?
am 23.02.2006 18:24:22 von tomAtLinux
--------------enigA2C8A25EC0902B6507DF0F86
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Normally you connect like this:
-------------8<-------------
use DBI;
my $dbh = DBI->connect($connectionString, $login, $password);
&foo($dbh);
sub foo {
my $dbh = shift;
my $sth = $dbh->prepare($cmd);
}
-------------8<-------------
Did you thought about using mod_perl there Apache::AuthDBI and
Apache::DBI for persitent database connections and authification.
Tom
Mary Anderson wrote:
> Hi all,
>
> I am writing a Web app in perl/cgi/dbi which (1) attempts to verify
> logins by checking against database user passwords, and (2) follows good
> dbi practices by logging the user in once at the beginning of the session
> instead of logging in immediately prior to accessing the database.
>
> I am running afoul of CGI parameter passing conventions. I do( and
> find) the following
>
> use CGI "standard";
> use DBI;
>
> my $dbh;
> if ($condition){
> $dbh = $DBI:connect($connectionString, $login, $password);
> # find connection is successful at this poing
>
> sub fubar{
>
> my $sth=$dbh->prepare($cmd);
> # find that $dbh is undefined.
>
> I have tried passing the handle in as a ref parameter, using $dbhRef = /*dbh
> (maybe this one was done incorrectly) and tried passing a $dbhRef as a
> value on a hidden textfield. None of these work. I tend to get
> "not an array" when I try to do $$dbhref to dereference the handle.
>
> Is there any way to make the handle persist?
> Would I have this trouble if I used Mason?
>
> Thanks in advance,
> Mary Anderson
>
>
>
--------------enigA2C8A25EC0902B6507DF0F86
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFD/e/LkVPeOFLgZFIRAvRUAKC3AikEonhvw10LnLyjnWd66VshrQCf XweX
yTKAXZU24l2ERZePLY9y8k0=
=Yo7Q
-----END PGP SIGNATURE-----
--------------enigA2C8A25EC0902B6507DF0F86--
Re: Make database handle persist in CGI/DBI application?
am 23.02.2006 18:27:39 von Alexander
Your CGI is called for each request and then terminated. There is no way
to make the DBI handle persistant. (And by the way, if hiding the handle
in a hidden field would work, you would create a security hole large
enough for a 747.)
You need a permanently running server for a persistant handle. Think
about using FastCGI, Apache::Registry or, if you just have started your
application, a mod_perl handler.
Alexander
Mary Anderson wrote:
>Hi all,
>
> I am writing a Web app in perl/cgi/dbi which (1) attempts to verify
>logins by checking against database user passwords, and (2) follows good
>dbi practices by logging the user in once at the beginning of the session
>instead of logging in immediately prior to accessing the database.
>
> I am running afoul of CGI parameter passing conventions. I do( and
>find) the following
>
>use CGI "standard";
>use DBI;
>
>my $dbh;
>if ($condition){
> $dbh = $DBI:connect($connectionString, $login, $password);
># find connection is successful at this poing
>
>sub fubar{
>
> my $sth=$dbh->prepare($cmd);
># find that $dbh is undefined.
>
>I have tried passing the handle in as a ref parameter, using $dbhRef = /*dbh
>(maybe this one was done incorrectly) and tried passing a $dbhRef as a
>value on a hidden textfield. None of these work. I tend to get
>"not an array" when I try to do $$dbhref to dereference the handle.
>
>Is there any way to make the handle persist?
>Would I have this trouble if I used Mason?
>
>Thanks in advance,
>Mary Anderson
>
>
--
Alexander Foken
mailto:alexander@foken.de http://www.foken.de/alexander/
RE: Make database handle persist in CGI/DBI application?
am 23.02.2006 18:40:18 von ctantalo
I have run into this myself The scope of dbh doesn't exist in your sub=
function I make the my $dbh declaration to an our $dbh This s=
eems to
do the trick for me.
=0AChris
-----------------=
------------------------------
Just Your Friendly Neighborhood
_S=
PIDEY_
> -----Original Message-----
> From: Mary And=
erson [mailto:mfanderson@ucdavisedu]
> Sent: Thursday, February 23=
, 2006 12:16 PM
> To: dbi-users@perlorg
> Subject: Make databa=
se handle persist in CGI/DBI application?
>
>
> Hi all,=0D=
=0A>
> I am writing a Web app in perl/cgi/dbi which (1) attempts =
> to verify logins by checking against database user passwords, =0D=
=0A> and (2) follows good dbi practices by logging the user in
> once=
at the beginning of the session instead of logging in
> immediately =
prior to accessing the database.
=0A>
> I am running afoul of =
CGI parameter passing conventions I do( and
> find) the following=
>
> use CGI "standard";
> use DBI;
>
> my $dbh;=
> if ($condition){
> $dbh =3D $DBI:connect($connectionStrin=
g, $login,
> $password); # find connection is successful at this poi=
ng
>
> sub fubar{
>
> my $sth=3D$dbh->prepare($c=
md);
> # find that $dbh is undefined.
=0A>
> I have tried pa=
ssing the handle in as a ref parameter, using
> $dbhRef =3D /*dbh (ma=
ybe this one was done incorrectly) and
> tried passing a $dbhRef as a=
value on a hidden textfield
> None of these work I tend to g=
et "not an array" when I try
> to do $$dbhref to dereference the han=
dle.
=0A>
> Is there any way to make the handle persist?
> W=
ould I have this trouble if I used Mason?
>
> Thanks in advance,=
> Mary Anderson
>
----------------------------------=
-------
The information contained in this message may be privileged,=
confidential, and protected from disclosure If the reader of this=
message is not the intended recipient, or any employee or agent
=
responsible for delivering this message to the intended recipient,
you=
are hereby notified that any dissemination, distribution, or
copying =
of this communication is strictly prohibited If you have
received t=
his communication in error, please notify us immediately
by replying t=
o the message and deleting it from your computer.
=0AThank you=
Paychex, Inc.
=0A