wittya
August 27th, 2003, 10:49 PM
The following scripts is supposed to change directories into /www where I have all of my .htm files and open and read each one of them to find the keyword that the user inputed on the search page and then return the web pages that have that keyword. I feel that I am close to getting it to work but I had a couple of questions. Do I have to define keywords inside of each of my .htm files? Also do you see anything blatantly wrong with the modified script. All of my .htm files are in my /www absolute folder. I keep on receiving that "Sorry, nothing this time" and then ships me back to my search page.
I am up for any new ideas.
#!/usr/bin/perl
# The following code deals with the form data
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
# Load the FORM variables
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
}
#Set the form input to a variable
$keyword=$FORM{keyword};
#Start the HTML with some search engine type words
print "Content-type: text/html\n\n";
print "<h2>Here are the files we found</h2>\n\n";
#Change the directory to the one you want to search - use absolute path
chdir("/www");
#Open it
opendir(DIR, ".");
#Start some loops that look for files with .htm
while($file = readdir(DIR))
{
next if ($file !~ /.htm/);
open(FILE, $file);
$foundone = 0;
$title = "";
while (<FILE>)
{
#If the keyword is found, set $foundone to one
if (/$keyword/i)
{
$foundone = 1;
}
#If there is a title, chop it and take the text between the two flags
if(/<TITLE>/)
{
chop;
$title = $_;
$title =~ s/<TITLE>//g;
$title =~ s/<\/TITLE>//g;
}
#No title? Fine. Use the file name
}
if($title eq "")
{
$title = $file;
}
#Print the title and file name so it is a link back to the file, set $listed to 1
if($foundone)
{
print "<A HREF=/$file>$title</A><BR>";
$listed=1;
}
close(FILE);
}
#Close the directory after looping through all the files
closedir(DIR);
# Print one line if no results, another if results were found
if($listed ne 1)
{print "Sorry, nothing this time. <A HREF=/index.htm>Try again</A>";}
else
{print "<P>That's all. Do you want a <A HREF=/index.htm>new search?</A>";}
exit;
I am up for any new ideas.
#!/usr/bin/perl
# The following code deals with the form data
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
# Load the FORM variables
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
}
#Set the form input to a variable
$keyword=$FORM{keyword};
#Start the HTML with some search engine type words
print "Content-type: text/html\n\n";
print "<h2>Here are the files we found</h2>\n\n";
#Change the directory to the one you want to search - use absolute path
chdir("/www");
#Open it
opendir(DIR, ".");
#Start some loops that look for files with .htm
while($file = readdir(DIR))
{
next if ($file !~ /.htm/);
open(FILE, $file);
$foundone = 0;
$title = "";
while (<FILE>)
{
#If the keyword is found, set $foundone to one
if (/$keyword/i)
{
$foundone = 1;
}
#If there is a title, chop it and take the text between the two flags
if(/<TITLE>/)
{
chop;
$title = $_;
$title =~ s/<TITLE>//g;
$title =~ s/<\/TITLE>//g;
}
#No title? Fine. Use the file name
}
if($title eq "")
{
$title = $file;
}
#Print the title and file name so it is a link back to the file, set $listed to 1
if($foundone)
{
print "<A HREF=/$file>$title</A><BR>";
$listed=1;
}
close(FILE);
}
#Close the directory after looping through all the files
closedir(DIR);
# Print one line if no results, another if results were found
if($listed ne 1)
{print "Sorry, nothing this time. <A HREF=/index.htm>Try again</A>";}
else
{print "<P>That's all. Do you want a <A HREF=/index.htm>new search?</A>";}
exit;