精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● CGI>>实例分析>>网站全文检索源代码(仅供参考)

主题:网站全文检索源代码(仅供参考)
发信人: jaron()
整理人: workingnow(2002-09-30 12:01:16), 站内信件
#!/usr/local/bin/perl
#####################################################################
# 在 http://www.jdinfo.net/cgi-bin/search.cgi 可以看演示!
$basedir    = "/usr/home/wuXXXX/www/"; # 要搜索的目录路径(绝对路径)
$baseurl    = "http://www.jdinfo.net/";
$title      = "江都资讯网";
$title_url  = "http://www.jdinfo.net/"; # 网址
$searchlogo =" http://www.jdinfo.net/images/searchlogo.jpg"; # 图标
$adjs = "http://www.jdinfo.net/jsscript/randomad.js"; # 广告显示
$search_url = "http://www.jdinfo.net/";
@files      =('*.htm','*.html','*/*.htm','*/*.html','*/*/*.htm','*/*/*.html','*/*/*/*.htm','*/*/*/*.html'); # 要搜索的文件类型
#####################################################################
&parse_form;
&get_files;
&search;
&return_html;

sub parse_form {
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   @pairs = split(/&/, $buffer);

   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;
   }
}

sub get_files {

   chdir($basedir);
   foreach $file (@files) {
      $ls = `ls $file`;
      @ls = split(/\s+/,$ls);
      foreach $temp_file (@ls) {
         if (-d $file) {
            $filename = "$file$temp_file";
            if (-T $filename) {
               push(@FILES,$filename);
            }
         }
         elsif (-T $temp_file) {
            push(@FILES,$temp_file);
         }
      }
   }
}

sub search {

   @terms = split(/\s+/, $FORM{'terms'});

   foreach $FILE (@FILES) {

      open(FILE,"$FILE");
      @LINES = <FILE>;
      close(FILE);

      $string = join(' ',@LINES);
      $string =~ s/\n//g;
      if ($FORM{'boolean'} eq 'AND') {
         foreach $term (@terms) {
            if ($FORM{'case'} eq 'Insensitive') {
               if (!($string =~ /$term/i)) {
                  $include{$FILE} = 'no';
     last;
               }
               else {
                  $include{$FILE} = 'yes';
               }
            }
            elsif ($FORM{'case'} eq 'Sensitive') {
               if (!($string =~ /$term/)) {
                  $include{$FILE} = 'no';
                  last;
               }
               else {
                  $include{$FILE} = 'yes';
               }
            }
         }
      }
      elsif ($FORM{'boolean'} eq 'OR') {
         foreach $term (@terms) {
            if ($FORM{'case'} eq 'Insensitive') {
               if ($string =~ /$term/i) {
                  $include{$FILE} = 'yes';
                  last;
               }
               else {
                  $include{$FILE} = 'no';
               }
            }
            elsif ($FORM{'case'} eq 'Sensitive') {
               if ($string =~ /$term/) {
  $include{$FILE} = 'yes';
                  last;
               }
               else {
                  $include{$FILE} = 'no';
               }
            }
         }
      }
      if ($string =~ /<title>(.*)<\/title>/i) {
         $titles{$FILE} = "$1";
      }
      else {
         $titles{$FILE} = "$FILE";
      }
   }
}
      
sub return_html {
print "Content-type: text/html\n\n";
print "<html>\n <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\n<title>搜索结果</title>\n \n";
print qq~
<style>.main        { font-style: normal; font-variant: normal; font-weight: normal; line-height: 
               15px; font-size: 9pt; font-family: 宋体 }
a:link       { text-decoration: none; font-size: 9pt ;color: #000000}
a:visited    { text-decoration: none; font-size: 9pt ;color: #000000}
a:active     { text-decoration: none; font-size: 9pt}
a:hover      { text-decoration: underline; color: #FF0000 }
body, table  { font-size: 9pt }
tr, td       { font-size: 9pt }
</style>
</head>
<body>
<div align="center">
<center>
<form method="POST" action="search.cgi">
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr>
<td align="left"><img border="0" src="$searchlogo" width="200" height="42"></td>
<td align="right"><script src="$adjs"></script></td>
</tr>
<tr><td colspan="2" height="15" valign="top"></td></tr>
<tr><td bgcolor="#EBEBEB">>> 位置: <a href="$baseurl">$title >> 全文检索</td><td bgcolor="#EBEBEB" align="right"><a href="$search_url">回到检索页</td></tr>
<tr><td colspan="2"></td></tr>
<tr><td colspan="2" align="left" bgcolor="#66CCFF" valign="bottom">
<p class="main">  填入查询内容:<input type="text" name="terms" style="width: 115; background-color: #FFFF99; color: #FF0000" size="22"> 
<input type="hidden" name="case" value="Insensitive"> <select size="1" name="boolean" style="background-color: #FFFF99"><option value="AND">= AND=</option><option value="OR">=O  R=</option>  </select> <input type="submit" value="继续搜索" style="font-family: 新细明体, 宋体, Arial; font-size: 9pt; background-color: #ffff99; color: 000000; text-align: center; vertical-align: middle; border: 1 double" padding-bottom:1px" onmouseover="this.style.color='#ff0000'" onmouseout="this.style.color='#000000'"></p>
</td>
</tr>
<tr><td colspan="2" align="left" height="25">您输入的关健字:~;
$i = 0;
   foreach $term (@terms) {
if ($term ne "") {
print qq~$term<br></td></tr>~;
}
else {
print qq~(您没有输入查询关键字)<br></td></tr>~;}
  $i++;
      if (!($i == @terms)) {
         print ", ";
  }
   }
   foreach $key (keys %include) {
$match++;
  if ($include{$key} eq 'yes') {
if ((int($match/2))!=($match/2)) {
print qq~<tr><td colspan="2" bgcolor="#FFFFFF">
  • <a href="$baseurl$key">$titles{$key}</td></tr>~;
    }
    else {print qq~<tr><td colspan="2" bgcolor="#F9F9F9">
  • <a href="$baseurl$key">$titles{$key}</td></tr>~;}
          }
       }
    print qq~<td colspan="2"></td>
    <tr><td colspan="2"><hr color="#0000FF" size="1" noshade></td></tr>
    <tr><td colspan="2"><script src="http://www.jdinfo.net/script/system_set.js"></script></td></tr>
    </table></form></center></div></body></html>
    ~;
    }



    ----
    欢迎下载使用 网站新闻管理系统 v2.2
    http://www.jdinfo.net/download
    http://www.jdinfo.net/demo (演示)
    oicq:942488 

  • [关闭][返回]