require "Sessions/mysql_session_handler.php"; // more on this later #### session_start(); #### $session = array(); session_register("session"); #### use CGI; use Apache::Session::MySQL; #### my $q = new CGI; my $sess_id = $q->cookie(-name=>'sess_id'); #### tie my %session, "Apache::Session::MySQL", $sess_id, { DataSource => 'dbi:mysql:sessions', UserName => 'root', Password => '', LockDataSource => 'dbi:mysql:sessions', LockUserName => 'root', LockPassword => '' }; #### my $cookie = $q->cookie(-name=>'sess_id',-value=>$session{'_session_id'}); print $q->header(-cookie=>$cookie); #### tied(%session)->delete; #### $session['_session_id'] = session_id(); #### #### #!/usr/bin/perl -w use strict; use DBI; use Serialize; # NOTE!!!! # The Serialize module is not found on CPAN # it was found with a google search; # you can download it here: # http://furt.com/code/perl/serialize/ use Storable qw(freeze thaw); my $id = $ARGV[0]; my $db = DBI->connect("DBI:mysql:sessions", "root", "" ); my $sel = $db->prepare("SELECT a_session FROM sessions WHERE id='$id'"); $sel->execute; my @data = $sel->fetchrow_array; if ( $data[0] ) { my $ref = thaw($data[0]); print "session|" . serialize($ref); } else { print ""; } $sel->finish; $db->disconnect; #### #!/usr/bin/perl -w use strict; use DBI; use Serialize; use Storable qw(freeze thaw); my $id = $ARGV[0]; my $data = $ARGV[1]; if ( $data eq '' ) { exit(0); } $data =~ s/^session\|//; my $ref = unserialize($data); $data = freeze($ref); my $db = DBI->connect("DBI:mysql:sessions", "root", "" ); my $sel = $db->prepare("SELECT a_session FROM sessions WHERE id='$id'"); $sel->execute; my @test = $sel->fetchrow_array; if ( $test[0] ) { $db->do( "UPDATE sessions SET a_session='$data' WHERE id='$id'" ); } else { $db->do( "INSERT INTO sessions VALUES ( '$id', '$data' )" ); } $sel->finish; $db->disconnect; print "1"; #### "; session_destroy(); // The session associative array is still hanging around, // however, it is no longer attached to the session, // so we can just unset it. unset ($session); } else if ( $username ) { // The script was given a username, so let's create a session. $session['username'] = $username; // This line is to appease our Perl counterpart. $session['_session_id'] = session_id(); session_register('session'); } if ( $user = $session['username'] ) { // There is a session defined already. Let's say hi. print "Hello $user!
"; print "Logout"; } else { // This is the first time we accessed the script. // Print out a text box print "
"; print "What is your username? "; print "
"; } ?> ##
## !/usr/bin/perl -w use strict; use CGI; use Apache::Session::MySQL; use CGI::Carp qw(fatalsToBrowser); my $q = new CGI; my $sess_id = $q->cookie(-name=>'sess_id'); # substitute the name of your session cookie here # These are the parameters for the session my $params = { DataSource => 'dbi:mysql:sessions', UserName => 'root', Password => '', LockDataSource => 'dbi:mysql:sessions', LockUserName => 'root', LockPassword => '' }; my %session; # The following lines tie %session to the session data # The script will die if we give it a $sess_id that doesn't exist. # So we put the tie call in an eval block. If there's an error # in $@ then we create a new session. eval { tie (%session, 'Apache::Session::MySQL', $sess_id, $params); }; tie (%session, 'Apache::Session::MySQL', undef, $params) if ( $@ ); if ( $q->param('action') ) { # We should delete the session. tied(%session)->delete; # Even though the session is deleted, the hash is still hanging around. # So we will just undef it, so that it doesn't confuse the script. undef %session; print $q->header; } elsif ( my $user = $q->param('username') ) { # We just created a new session. $session{'username'} = $user; # Remember to create the cookie. my $cookie = $q->cookie(-name=>'sess_id', -value=>$session{_session_id}); print $q->header(-cookie=>$cookie); } else { print $q->header; } if ( $session{'username'} ) { # The session already exists. Say hello. print "Hello $session{'username'}!
"; print "Logout"; } else { # Print login box print "
"; print "What is your username? "; print "
"; }