My server/host does’t have a CGI bin. So can anyone just put this textdb.pl file on their site. And then I can link to it. Thanks in advance!
http://www.yoursite.com/cgi-bin/textdb.pl
Paste this into textdb.pl
#! /usr/local/bin/perl
#-------------------------------------------------------------------------------
Name: Text DB
Version: 1.1.1
Author: Derek Clayton derekc@iceinc.com
Description: Flat File Database - Tab Delimited
#-------------------------------------------------------------------------------
PATHS
#-------------------------------------------------------------------------------
local $pathDirDb = ‘data/’; # server path to the database directory
local $pathDirTemp = ‘data/’; # server path to the database temp dir
# which is used for updating the db
local $pathDirDetails = ‘data/’; # server path to database details dir
local $pathDirLockDb = ‘data/’; # server path to lockfile dir used for
# managing database access
local $pathDirLockDet = ‘data/’; # server path to lockfile dir used for
# managing details access
#-------------------------------------------------------------------------------
There is no need to alter code below this point!
#-------------------------------------------------------------------------------
GLOBALS
#-------------------------------------------------------------------------------
local $outputString = ‘’; # string to return to the client
local $status = 0; # status of the request
local $pathFileDb = ‘’; # server path to the database file
local $pathFileDetails = ‘’; # server path to the database details file
local $pathFileTemp = ‘’; # server path to the database temp file
local $pathFileLockDb = ‘’; # server path to the database lock file
local $pathFileLockDet = ‘’; # server path to the details lock file
local $detailsRID; # keeps track of record id’s
local @index; # the field index array
local $lenIndex; # the length of the local index array
local $LOCK_SH = ‘1’; # shared locking
local $LOCK_EX = ‘2’; # exclusive locking
local @res_words = (‘command’, ‘’, ‘database’, ‘RID’, ‘start’, ‘finish’);
# reserved words which cannot be field names
#-------------------------------------------------------------------------------
MAIN
#-------------------------------------------------------------------------------
use CGI; # use the CGI.pm for easy parsing
local $query = new CGI; # query object
&writeHeader; # sends a header to flash
&handleRequest; # determine the appropriate handler for the request
&writeFooter; # sends a footer to flash
&sendOutput; # send the output to the client
exit; # exit the script
#-------------------------------------------------------------------------------
write the header
sub writeHeader {
print "Content-type: application/x-www-urlform-encoded
";
}
determine the appropriate handler for the request
sub handleRequest {
# get the database name to use or fail
local $database = $query->param(‘database’);
&error(8) unless($database);
# get the command requested or fail
my $request = $query->param('command');
&error(7) unless($request);
# set the path to the database file
$pathFileDb = $pathDirDb . $database . '.txt';
$pathFileDetails = $pathDirDetails . $database . '.det';
$pathFileTemp = $pathDirTemp . $database . 'temp.txt';
$pathFileLockDb = $pathDirLockDb . $database . '.lck';
$pathFileLockDet = $pathDirLockDet . $database . '.lck';
# if the db file does not exist...
if (!(-e "$pathFileDb")) {
# ...and we want to add a record then create the db
if($request eq 'addRecord') {
&dbCreate;
} else {
&error(9);
}
}
# process the request based on the command given
if ($request eq 'addRecord') {
&processAdd;
} elsif ($request eq 'getNumRecords') {
&processGetNumRecords;
} elsif ($request eq 'getRecords') {
&processGetRecords;
} elsif ($request eq 'updateRecord') {
&processUpdateRecord;
}
}
write the footer
sub writeFooter {
# add the status of the request
$outputString .= “status=$status”;
}
send the output to the client
sub sendOutput {
# send the output result to the client
print $outputString;
}
main process for adding a record to the db
sub processAdd {
# get the latest record ID
&getNewRecordID;
# load the index
&indexLoad;
# add the record
&recordAdd;
}
main process for retrieving the number of records in the db
sub processGetNumRecords {
# set to -1 so as not to count the first record which is record index
my $count = -1;
# lock access
&error(10) unless (open(LCK_DB, ">$pathFileLockDb"));
flock(LCK_DB, $LOCK_SH);
# open the database
&error(2) unless (open(DB, "$pathFileDb"));
# for each record add to the count
while(<DB>) {
$count++;
}
# close the database
close(DB);
# unlock access
close(LCK_DB);
# append the result to the output
$outputString .= "numRecords=$count&";
}
main process for retrieving records in the db
sub processGetRecords {
local $lenIndex = 0;
# load the index
&indexLoad;
# get the records
&recordReturn;
}