#create the variable to make the connection to the SQL
server, so whenever you need to connect you call the
$dbh variable
my $dbh =
DBI->connect"DBI:mysql:database=gns_workflow;host=localhost",'root',
'MySQL!') or die "didnt connect to the database";
#putting the $dbh into the $self object, that way you
can call $dbh anywhere in the program by issuing the
command $self->param('dbh')
my $self->param('dbh' => $dbh) or die "couldnt
associate dbh variable to self";
Attached is the entire program... Please take a look
and advise on any improvements. I am just learning so
take it easy on me ;0)
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
package Portal::Application_2;
use base 'CGI::Application';
use CGI::Session();
use DBI;
use strict;
sub cgiapp_init
{
my $self=shift;
#cgi::application uses the cgi.pm module. $self-_query() is equal to
$q=CGI->new()
my $q=$self->query();
#create the variable to make the connection to the SQL server, so
whenever you need to connect you call the $dbh variable
my $dbh =
DBI->connect("DBI:mysql:database=gns_workflow;host=localhost",'root', 'MySQL!')
or die "didnt connect to the database";
#putting the $dbh into the $self object, that way you can call $dbh
anywhere in the program by issuing the command $self->param('dbh')
my $self->param('dbh' => $dbh) or die "couldnt associate dbh variable
to self";
# Initialize the session and get the id.
my $session = CGI::Session->new
(
'driver:MySQL',
$q->cookie('CGISESSID') || $q->param('CGISESSID') || undef,
{
Handle=>$dbh,
LockHandle=>$dbh
}
)or die "couldn't establish session";
#sets the expire time for the session. It only expires after that
amount of time after inactivity. Every time the user does something the expire
time is reset.
$session->expire("10m");
#put the session id into the $sessionid variable
my $sessionid = $session->id();
# Put session id and into $self object
$self->param('sessionid'=>$sessionid);
# Put session into $self object
$self->param('session'=>$session);
&login_form;
}
#when using CGI::Applicaiton you need to define the basics of your application.
For example, what the rum modes are, and which run mode that should be started
in first.
sub setup
{
#Here we are getting the object. Think of it as a box that is passed
around from one person to another, you can put stuff in and take stuff out of
the box.
#We have just accepted the "box" by typing "$self=shift;".
#The "shift" function removes the first value of the specified array.
If you dont specify an array the function shifts @ARGV (in the main program),
or @_ (in subroutines)
#Since we didnt specify an array, and are in a subroutine we are
grabbing the @_
#Within a subroutine the array @_ contains the parameters passed to
that subroutine, and is considered a special variable
my $self = shift;
#cgi::application is based on the idea that everything a web
application does is a mode. Basically what this breaks down to is a mode is
simply associated with a subroutine.
# I find it simpler to just use the subroutine name for both the mode
name and the subroutine, however the directions show ('mode1' =>
'subroutine_name')
$self->run_modes
(
'mode_1' => 'login_form',
'mode_2' => 'access_denied',
'mode_3' => 'validate_user',
'mode_4' => 'main_page'
);
#Here we are picking the mode we want to run first
$self->start_mode('mode_1');
}
sub access_denied
{
return "Sorry, your access has been denied. Please make sure you have
permissions to perform that function.";
}
sub login_form
{
my $self = shift;
my $login_form = <<ENDHTML;
<FORM ACTION=index.pl METHOD="POST">
<TABLE>
<TR>
<TD>Username:</TD>
<TD><INPUT TYPE="text" NAME="username" SIZE=20></TD>
</TR>
<TR>
<TD>Password:</TD>
<TD><INPUT TYPE="password" NAME="password" SIZE=20></TD>
</TR>
<TR>
<TD><INPUT TYPE="hidden" NAME="rm" VALUE="mode_4"></TD>
</TR>
</TABLE>
<INPUT TYPE="submit" VALUE="Submit">
</FORM>
ENDHTML
}
sub validate_user
{
my $self = shift;
#Putting the "username submitted in the HTML Form into the $username
variable.
my $username = $self->query("username");
#Putting the "password" submitted in the HTML Form into the $password
variable
my $password = $self->query("password");
###SQL Query
my $password_query = "SELECT * FROM user WHERE username = ? AND
password = ?";
my $sth = $self->param('dbh')->prepare($password_query);
my $res = $sth->execute($username, $password)->fetchrow_hashref();
if ($res->{username} eq $username)
{
return "hello world";
} else {my $return = "Please click the back button and enter a username
and password. Both fields must be filled out.";}
}
sub main_page
{
my $self = shift;
my $hereyouare = "Welcome to the new GNS portal!";
$hereyouare;
}
1;
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/[email protected]/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]