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();
##
##
function mysql_session_open ( $save_path, $session_name )
{
// Since all database editting is done in Perl scripts
// these functions do nothing...
return true;
}
function mysql_session_close ( )
{
return true;
}
function mysql_session_read ( $id )
{
return `perl /usr/share/php/Sessions/perl_get_serialized_session.pl $id`;
}
function mysql_session_write ( $id, $serialized )
{
return `perl /usr/share/php/Sessions/perl_save_serialized_session.pl $id '$serialized'`;
}
function mysql_session_destroy ( $id )
{
$db = mysql_connect("localhost", "root");
mysql_select_db('sessions', $db);
return mysql_query("DELETE FROM sessions WHERE id='$id'");
}
function mysql_session_gc ( $maxlife )
{
// Currently there is no good garbage collection
// Be sure to call "session_destroy()" in PHP scripts
return true;
}
session_set_save_handler (
'mysql_session_open',
'mysql_session_close',
'mysql_session_read',
'mysql_session_write',
'mysql_session_destroy',
'mysql_session_gc' );
?>
##
##
#!/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";
##
##
require "Sessions/mysql_session_handler.php"; // put in /usr/share/php
session_start();
if ( $action = "logout" )
{
// Someone hit the logout button. Destroy the session.
print "Session destroyed.
";
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 "";
}
?>
##
##
!/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 "";
}