Hello Monks,

I use flat text database Perl script. It search all or part of online database and display the results in the html template(table based). There is one problem: how to set the script not to display all the database when you just type in the URL? I mean to prevent database from listing/viewing.
When the user type in browser http://webhost/cgi-bin/csvsearch.pl or http://webhost/cgi-bin/database.txt, it get all database at once, all database drops out at once! Its not look very nice.

#!/usr/bin/perl ########################################################## my $CSV_file = "database.txt"; my $HTML_template = "template.htm"; my $no_matches_found = "Sorry, no results found"; my $ID_use = 0; my $ID_field_name = "ID"; ########################################################## #%FORM = parse_cgi(); print "Content-type: text/html\n\n"; (my $head, my $tmp, my $foot) = get_html($HTML_template); $qs=$ENV{'QUERY_STRING'}; ##read db my @data = read_file($CSV_file); chomp $data[0]; my @fields= split('\|', shift @data); $base_length = @data; error("You have bad file!") if !@fields; error("Database is clear!") if $base_length<1; if($qs =~m/header=([^\&\Z]*)/){push @header,$1;} if($ID_use && $qs =~m/show=([^\&\Z]*)/){ @data = search($1,$ID_field_name); } else{ @conditions=split(/&/,$qs); my $a=0; foreach (@conditions){ ($name, $value) = split(/=/, $_); if($name eq 'search'){ $FORM{search} = $value; @data = search($value, $header[0]); } elsif($name eq 'header'){} elsif($_=~/([^=<>!]+)!=([^=<>!]+)/){@data = search($value, $1, + "!=");} elsif($_=~/([^=<>!]+)=([^=<>!]+)/){@data = search($value, $1); +} $a++; } } my $result; ##matched data foreach(@data){ chomp; @line = split('\|', $_); $a=0; %INSERT=(); foreach(@fields){$INSERT{$_} = $line[$a++];} $result.=get_record($tmp) } %INSERT=(); $INSERT{'#_matches'} = @data; $INSERT{'#_total'} = $base_length; $result = $no_matches_found."<br>" unless @data; print get_record($head), $result, get_record($foot); undef $result; undef $head; undef $foot; exit; ######################################################### sub search{ my $word=shift; my $field=shift; my $action=shift; $word=~tr/+/ /; $word=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg; my $position=-1; my $a=0; if($field){ $field=~tr/+/ /; $field=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg; foreach(@fields){$position=$a if $_ eq $field; $a++;} } my %match; $word =~s/ +/ /g; my @new_data=(); my @keys= split(" ", $word); if($action eq '!='){for(0..@data-1){$match{$_} = 1;}} foreach $key (@keys){ $a=0; foreach $record (@data){ @line = split('\|', $record); if($field && $position>-1){ $match{$a} = ($line[$position] =~ m/\b\Q$key\E\b/i +) ? 1 : 0; $match{$a} = !$match{$a} if $actio +n eq '!='; } else{ foreach(@line){if ($_=~m/\Q$key/i){$match{$a} = 1; las +t;}} } $a++; } } $a=0; my $b=0; foreach(@data){ $new_data[$b++] = $_ if $match{$a}; $a++; } return @new_data; } sub get_record{ my $text = $_[0]; $text =~ s{<<(.*?)>>}{exists($INSERT{$1}) ? $INSERT{$1} : ""}gsex; return $text; } sub get_html{ my @txt = read_file($_[0]); my $txt; foreach(@txt){$txt.=$_;} $txt=~/(.*)<template>(.*)<\/template>(.*)/s; error("Template-tag not found!") if !$1 or !$2; return ($1,$2,$3); } sub read_file{ open(F, $_[0]) || error("Can't open file $_[0]!"); my @data = <F>; close F; return @data; } sub error{ print "<html><head><title>Error</title>$style</head><body><br><br> +<br><font color=red><h3>$_[0]</h3></font></body></html>"; exit; } ##########################################################

2004-12-03 Janitored by Arunbear - added readmore tags, as per Monastery guidelines


In reply to How to prevent database from listing/viewing? by santander

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.