Help Me Out!

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;

}

main process for updating a record

sub processUpdateRecord {
local $lenIndex = 0;

# load the index
&indexLoad;

# lock access
&error(10) unless (open(LCK_DB, ">$pathFileLockDb"));
flock(LCK_DB, $LOCK_EX);

# open the database
&error(20) unless (open(DB, "$pathFileDb"));

# open the temporary file
&error(4) unless (open(TMP, ">$pathFileTemp"));

# get the record ID to update
my $recordID = $query->param('RID');

# search through the database
while(<DB>) {
    # remove the newline from each record
    chomp($_);

    # load the individual field values into an array
    my @fields = split("	",$_);

    # if the record ID of the current field is equal to the record we would
    # like to update...
    if($fields[0] == $recordID) {
        # ... get the new values from the input
        foreach $key ($query->param) {
            # if it's a field value and not a reserved key...
            if (!&reserved_word($key)) {
                # ...find out where in the index this key occurs
                for ($i=1;$i<$lenIndex;$i++) {
                    # if found...
                    if ($index[$i] eq $key) {
                        # ... set the field to the input value
                        $fields[$i] = $query->param($key);
                    }
                }
            }
        }

        # create the whole record from the individual fields
        local $record = join("	",@fields);

        # write the record to the temporary database
        print TMP "$record

";
} else {
# if not the record ID we are updating then simply write the
# record to the database
print TMP "$_
";
}
}

# make the temporary database our new database
&error(12) unless(rename($pathFileTemp, $pathFileDb));

# close our file handles
close(DB);
close(TMP);

# unlock access
close(LCK_DB);

}

gets and then increments the next available recordID

sub getNewRecordID {
# lock access
&error(11) unless (open(LCK_DET, “>$pathFileLockDet”));
flock(LCK_DET, $LOCK_EX);

# open the details file
&error(6) unless (open(DET, "+<$pathFileDetails"));

# remove the newline from the record and assign the value
chomp($detailsRID = <DET>);

# increment the record id counter
$detailsRID++;

# set the file pointer to the beginning of the file
seek(DET, 0, 0);

# write to the details file
print DET "$detailsRID

";

# truncate the rest of the file
truncate(DET, tell(DET));

# close the details file handle
close(DET);

# unlock access
close(LCK_DET);

}

loads the index array

sub indexLoad {
# lock access
&error(10) unless (open(LCK_DB, “>$pathFileLockDb”));
flock(LCK_DB, $LOCK_SH);

# open the database
&error(2) unless (open(DB, "$pathFileDb"));

# read the index record
$line = <DB>;

# delete the newline
chomp($line);

# load the fields into an array
@index = split("	",$line);

# set the number of fields in the index
$lenIndex = @index;

# close the file handle
close(DB);

# unlock access
close(LCK_DB);

}

writes the record

sub recordAdd {
my @fields;
my $isIndexChanged = 0;
my $isFound = 0;

# for each input key
foreach $key ($query->param) {
    # if it's not a reserved key but can be equal to RID
    if (!&reserved_word($key) || ($key eq 'RID')) {
        $isFound = 0;
        # find out where in the index this key occurs
        for ($i=1;$i<$lenIndex;$i++) {
            # if found...
            if ($index[$i] eq $key) {
                # ...set the field value to the input value
                $fields[$i] = $query->param($key);
                $isFound = 1;
            }
        }

        # if the key was not found...
        if($isFound == 0) {
            # ...add the key to the index...
            $isIndexChanged = 1;
            $index[$lenIndex] = $key;

            # ...and the value to the fields
            $fields[$lenIndex] = $query->param($key);

            # increment the length of the index
            $lenIndex++;
        }
    }
}

# add the record ID
$fields[0] = $detailsRID;

# create the whole record from the individual fields
my $record = join("	",@fields);

# lock access
&error(10) unless (open(LCK_DB, ">$pathFileLockDb"));
flock(LCK_DB, $LOCK_EX);

# open the database
&error(3) unless (open(DB, ">>$pathFileDb"));

# write the record
print DB "$record

";

# close the filehandle
close(DB);

# unlock access
close(LCK_DB);

# if the index has changed because a new field was added then update the
# index record
if($isIndexChanged == 1) {
    &updateIndex;
}

}

updates the index record

sub updateIndex {
# lock access
&error(10) unless (open(LCK_DB, “>$pathFileLockDb”));
flock(LCK_DB, $LOCK_EX);

# open the database
&error(2) unless (open(DB, "$pathFileDb"));

# open the temporary file
&error(4) unless (open(TMP, ">$pathFileTemp"));

# join the new index into a single record
my $index = join("	", @index);

# print the new index as the first record to the temp file
print TMP "$index

";

# read the old index and do nothing
<DB>;

# read the rest of the file and copy it to the temp file
while(<DB>) {
    print TMP $_;
}

# close the database file handle
close(DB);

# close the temporary database file handler
close(TMP);

# make the temporary database our new database
&error(11) unless(rename($pathFileTemp, $pathFileDb));

# unlock access
close(LCK_DB);

}

returns records from the db

sub recordReturn {
# lock access
&error(10) unless (open(LCK_DB, “>$pathFileLockDb”));
flock(LCK_DB, $LOCK_SH);

# open the database
&error(2) unless (open(DB, "$pathFileDb"));

my $count = 0;      # set counter to 0
my $isDone = 0;       # set done to false
my $line = <DB>;    # read a record from our database

# while more records and we haven't passed the maximum record wanted
while(defined($line) && ($isDone == 0)) {
    # if this record is wanted
    if (($count >= $query->param('start')) &&
        ($count <= $query->param('finish'))) {
        # delete the newline
        chomp($line);

        # load the fields into an array
        @fields = split("	",$line);

        # add the keys and values to the output
        for ($i=0;$i<$lenIndex;$i++) {
            $outputString .= "record$count";
            $outputString .= "$index[$i]=$fields[$i]&";
        }
    } elsif ($count >= $query->param('finish')) {
        # if we have exceed the maximum record wanted then we're done
        $isDone = 1;
    }

    # incremement our count
    $count++;

    # get the next record
    $line = <DB>;
}

# close the filehandle
close(DB);

# unlock access
close(LCK_DB);

}

create the db including index record

sub dbCreate {
# lock access
&error(11) unless (open(LCK_DET, “>$pathFileLockDet”));
flock(LCK_DET, $LOCK_EX);

# create the index file
&error(5) unless (open(DET, ">$pathFileDetails"));

# initialize it so next record ID used is 1
print DET "0

";

# close the details file handle
close(DET);

# unlock access
close(LCK_DET);

# lock access
&error(10) unless (open(LCK_DB, ">$pathFileLockDb"));
flock(LCK_DB, $LOCK_EX);

# create the database file
&error(1) unless (open(DB, ">$pathFileDb"));

# create the index record
my $record = "RID	";

# for each key given create an index field...
foreach $key ($query->param) {
    # ... only if it's not a reserved word
    if(!&reserved_word($key)) {
        $record .= "$key	";
    }
}

# write to the database file
print DB "$record

";

# close the file handle
close(DB);

# unlock access
close(LCK_DB);

}

tests to see if a word is reserved…returns true if yes, false if no

sub reserved_word {
my $test_word = @_[0];

# for each reseved word...
foreach $word (@res_words) {
    # ...if our test word equals a reserved word return true
    if($word eq $test_word) {
        return 1;
    }
}

# if our word does not match any reserved words return false
return 0;

}

sub error {
$status = @_[0];
&writeFooter;
&sendOutput;
exit;
}

[AS]

I suggest opening up a madsims.net account and using that for all your CGI purposes.

I need a free host thats has a cgi bin.

AsianIllusion.com :wink:

http://www.xxviii.net/cgi-bin/textdb.pl

Doesnt show Maybe you need to do this. Thanks dude. You rock! I have the hitcounter.swf on my server. Does it need to be on urs?

Place textdb.pl in a CGI-enabled directory of your web site.
Make textdb.pl executable (typically, this means setting the file’s permissions to 755).
On Unix, set the path to the Perl interpreter on the first line of textdb.pl. By default the location is set to:
#! /usr/local/bin/perl
Download and extract moockHitCounter.zip.
Open hitcounter.fla in Flash MX or later.
On frame 6 of the “scripts” layer, specify the location of textdb.pl on your site. For example,
var scriptURL = “http://www.yoursite.com/cgi-bin/textdb.pl”;
Optionally, you may also specify the name of your hit counter database. For example:
var dbName = “flashpagehits”;
By specifying different database names, you can use textdb.pl to track hits for multiple flash movies.
Export your movie (File >> Publish).
Place the exported hitcounter.swf file on your website.
Watch in fascination as the hits accrue.

well i CHMODDED it to 755…
see if it works now… im gettin confused as to what you need :confused:
sorry… tryin to help, just kinda slow today

Nope, this might help. Here is the tutorial I used.
http://moock.org/webdesign/flash/actionscript/hitcounter/

Maybe You need to upload my hitcounter.swf to your server. So I can view it.
Thanks a bunch for your time!
Alex

*Originally posted by Voetsjoeba *
**AsianIllusion.com :wink: **

:love:

I believe with internet security to do stuff like this you need to host it on your local server and can’t host this remotely but I could be wrong.