Click to See Complete Forum and Search --> : Perl Search Script not working help


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;

venkitaraman
September 2nd, 2003, 02:09 AM
hello wittya,
actually iam also doing the same project but using asp. i have undestood that you are on the verge of getting output.so i suppose you can help in this case. i have lot of doubts:-

1)your html files are in a database in your machine or in the server.if it is in the server how had you established a connection to the server.

2)how to get the documents after search into an array and display it another web page.

if you know asp then i'll post my codings to you and you can help me to fix out my errors.
hope you will help me out
thanks and regds
venki

wittya
September 2nd, 2003, 08:25 PM
I had the wrong path for my search in my /www folder. I use a webhosting service so instead of /www I needed to grab the path infront of /www

#!/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>Top Search Results</h2>\n\n";


#Change the directory to the one you want to search - use absolute path
chdir("$ENV{'DOCUMENT_ROOT'}/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=javascript:history.back()>Try again</A>";}
else
{print "<P>That's all. Do you want a <A HREF=javascript:history.back()>new search?</A>";}





exit;



hope this help anyone else trying to figure this thing out:)

ali