Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Form validation/Search script

by Limbic~Region (Chancellor)
on Oct 24, 2005 at 18:49 UTC ( [id://502556]=note: print w/replies, xml ) Need Help??


in reply to Form validation/Search script

No-Lifer,
This is the first node I have seen you write on this topic, so I am unfamiliar with the history. It sounds like you are building a rudimentary search engine. It also sounds like you want to do this on your own instead of using a pre-built wheel.

There is nothing wrong with this approach in general, but it is also sometimes useful to learn about existing technology:

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Form validation/Search script
by No-Lifer (Initiate) on Oct 24, 2005 at 19:09 UTC
    Limbic-Region,

    Cheers for the reply. As to what I'm doing - spot on. It's actually a bit of coursework I'm working on for University - I have a mandatory "Introduction to perl/cgi" class which I'm sucking at- but determined to get finished quite soon.

    So, yes, I'm building a very simple search engine to go through a few pages (try www.ally.nu - searching for "perl"). I've got a few bits and bobs working, thanks to the other Monks here, and nearly have an application I could submit. Bearing in mind that they're not expecting miracles from us - we're not programming students!

    I'm trying to do it in the most straightforward way possible - this is my first experience with perl *shudder*.

    The things that're stumping me at the moment are - if "submit" is pressed without any form data, how to display an "error" page. And secondly, the question above - an "AND" type search.

    My full code is below - I know it's a complete mess, will tidy it up at the end! Thank goodness we're not being assessed on code pretty-ness, purely on search engine function!

    #!/usr/bin/perl -w # The following code deals with the form data if ($ENV{'REQUEST_METHOD'} eq 'POST') { 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; $value =~ s/["]//gi; $value =~ s/[+]/ /gi; $FORM{$name} = $value; } } $keyword=$FORM{keyword}; chdir("/home/1008/gnicoll/www.abernyte.net/public_html"); opendir(DIR, "."); print "Content-type: text/html\n\n"; print"<STYLE>"; print"BODY {FONT-FAMILY: arial,sans-serif}"; print"TD {FONT-FAMILY: arial,sans-serif}"; print"DIV {FONT-FAMILY: arial,sans-serif}"; print"P {FONT-FAMILY: arial,sans-serif}"; print"A {FONT-FAMILY: arial,sans-serif}"; print"UNKNOWN {COLOR: #0000cc}"; print"</STYLE>"; print"<BODY bgColor=#ffffff topMargin=2 marginheight=2>"; print"<TABLE cellSpacing=2 cellPadding=0 width=100% border=0>"; print"<TBODY>"; print"<TR>"; print"<TD width=1% height=69 vAlign=top><a href=http://www.ally.nu><IM +G height=59 alt=Go to Noogle Home hspace=3 src=http://www.ally.nu/logo.gif width=143 vspace=5 border=0></a></TD>" +; print"<TD width=868></TD>"; print"</TR>"; print"</TBODY>"; print"</TABLE>"; print"<TABLE cellSpacing=0 cellPadding=0 width=100% border=0>"; print"<TBODY>"; print"<TR>"; print"<TD bgColor=#3366cc><IMG height=1 width=1></TD></TR></TBODY></TA +BLE>"; print"<TABLE cellSpacing=0 cellPadding=2 width=100% border=0>"; print"<TBODY>"; print"<TR>"; print"<TD bgColor=#e5ecf9 colSpan=4><B>Search Results</B> - Your Searc +h for the keyword(s) <strong>$keyword</strong> returned the following results:</TD></TR></T +BODY></TABLE><BR>"; print"<TABLE cellSpacing=0 cellPadding=2 width=100% border=0>"; print"<TBODY>"; print"<TR>"; print"<TD width=133 rowspan=2 vAlign=top noWrap bgColor=#ffffff><P><SM +ALL><A href=http://www.ally.nu>Noogle Home</A><BR><BR><A href=http://www.ally.nu/docs>Documentation</A><BR>< +br><A href=http://www.ally.nu/credits>Credits</A><br><BR><A href=http://www. +ally.nu/docs/faq>FAQ<br></A><BR><A href=http://www.ally.nu/quiz>Quiz</A><BR>"; print"<BR><BR>"; print"</SMALL></P></TD>"; print"<TD width=1 height=37 vAlign=bottom></TD>"; print"<TD width=1 rowspan=2 vAlign=bottom background=http://www.ally.n +u/dot2.gif><IMG height=1 src=http://www.ally.nu/dot2.gif width=1></TD>"; print"<TD width=1 vAlign=bottom></TD>"; print"<TD width=100% valign=top><P><B><FONT size=-1>Search Results</FO +NT></B></P></TD>"; print"</TR>"; print"<TR>"; print"<TD height=598 vAlign=bottom></TD>"; print"<TD vAlign=bottom></TD>"; print"<TD valign=top></p>"; print"<p></p>"; print"<p></p>"; print"<p></p>"; while($file = readdir(DIR)) { next if ($file !~ /.html/); open(FILE, $file); $foundone = 0; $title = ""; while (<FILE>) { if (/$keyword/i) { $foundone = 1; } if(/<title>/) { chop; $title = $_; $title =~ s/<title>//g; $title =~ s/<\/title>//g; } if(/<TITLE>/) { chop; $title = $_; $title =~ s/<TITLE>//g; $title =~ s/<\/TITLE>//g; } if($title eq "") { $title = $file; } if(/<META NAME="description" CONTENT="/i) { chop; $content = $_; $content =~ s/<META NAME="description" CONTENT="//g; $content =~ s/">//g; } if(/<META NAME="author" CONTENT="/i) { chop; $author = $_; $author =~ s/<META NAME="author" CONTENT="//g; $author =~ s/">//g; } if($content eq "") { $content = "No Meta-tag page information available"; } if($author eq "") { $author = "No Meta-tag author information available"; } $count++ while /$keyword/ig; } if($foundone) { print "<A HREF=/$file>$title</A><br>"; print"<table width=100% border=0 align=center bgcolor=#e5ecf9>"; print"<tr>"; print"<td height=10><font size=-1><b>Results</b>: <i>$count</i> occurr +ence(s) of the word(s) <i>\"$keyword\"</i> on this page.<br> <b>Page Description</b>: $content<br><b>Page Author< +/b>: $author<br><b>URL</b>:<font color=#008000>http://www.ally.nu/$file</td>"; print"</tr>"; print"</table>"; print"<br>"; $count = 0; $listed=1; } close(FILE); } if($listed ne 1) {print "<p><br>Sorry, your search returned <b>$foundone</b> res +ults. <A HREF=/index.html>Search Again?</A>";} else {print "<P><br>Do you want a <A HREF=/index.html>new search?</A +>";} print"</TD>"; print"</TR>"; print"</TBODY>"; print"</TABLE>"; print"<BR>"; print"<CENTER>"; print"<TABLE cellSpacing=0 cellPadding=0 width=100% border=0>"; print"<TBODY>"; print"<TR>"; print"<TD bgColor=#3366cc><IMG height=1 width=1></TD></TR></TBODY></TA +BLE>"; print"<TABLE cellSpacing=0 cellPadding=2 width=100% bgColor=#e5ecf9 bo +rder=0>"; print"<TBODY>"; print"<TR>"; print"<TD noWrap bgColor=#e5ecf9>"; print"<TABLE cellSpacing=0 cellPadding=0 width=100% border=0>"; print"<TBODY>"; print"<TR>"; print"<TD noWrap align=middle><FONT size=-1>©2005 Noogle - Napier Univ +ersity Server Side Languages Coursework <A href=http://www.ally.nu>Noogle Home</A> - <A href=http://www.ally.nu/d +ocs>Documentation</A> - <A href=http://www.ally.nu/credits>Credits</A> - <A href=http://www.ally. +nu/docs/faq>FAQ</A> - <A href=http://www.ally.nu/quiz>Quiz</A></FONT></TD></TR></TBODY></TABLE> +</TD></TR></TBODY></TABLE></CENTER></BODY></ HTML>"; closedir(DIR); exit;


    I've also cannibalised quite a bit from other scripts - it's all cobbled together really. But at least I'm understanding what's happening! Cheers, NL.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://502556]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-28 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found