Forum Moderators: phranque
- Write form results to a comma-delimited text file
- The data in the text file needs to be secure so no unauthorized prying eyes can get at it.
- Easy to set up. I've tried a few that were pretty complex.
- Hopefully free (I'm on a limited budget!)
I'm not particular if the script is PERL or PHP, just so it works.
Does anyone have any suggestions?
Thanks,
Matthew
You can protect the directory so no one can download it, but you will be able to read it, and if it is going across the internet, it won't be secure unless you route it through an HTTPS uri.
I suppose you can encrypt the data as it is being written by running it through an excryption subroutine.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_FORM = $_POST;
}
else {
$_FORM = $_GET;
}
foreach ($_FORM as $key => $value) {
if ($key!= $value) {
$value = str_replace("''", "'", $value);
$value = str_replace("+", " ", $value);
$value = str_replace(",", "", $value);
filewrite ("$key,$value\n");
}
}
function filewrite ($toWrite) {
$fp = fopen ("../file.ext", "a");
fwrite ($fp, $toWrite, strlen ($toWrite));
fclose ($fp);
}
?>
--------
Perl:
#!/usr/bin/perl
use strict;
use warnings;
my $buffer;
my %FORM;
print "Content-type: text/html\n\n";
if ($ENV{'REQUEST_METHOD'} =~ /POST/i) {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
else {
$buffer = $ENV{'QUERY_STRING'};
}
my @pairs = split(/&/, $buffer);
foreach my $pair (@pairs) {
my($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\'\'/\'/g;
$value =~ s/\,//g;
$FORM{$name} = $value;
}
foreach my $key (keys(%FORM)) {
&filewrite("$key,$FORM{$key}\n");
}
sub filewrite {
my $toWrite = shift;
open(OUTF,">>../file.ext");
seek(OUTF, 0, 2);
print OUTF "$toWrite";
close(OUTF);
}
exit 1;
HTH
Jordan