Click here to Skip to main content
15,895,142 members
Articles / Web Development / XHTML

Exact Phrase Website Live Search Script .NET 3.5

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
18 Aug 2010CPOL5 min read 31.7K   429   22  
This script can be used in an ASP.NET 3.5 website. It can search out exact phrases and keywords live from all HTML pages and text files on the website.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="phrasesearch.aspx.cs" Inherits="cPhraseSearch" %>

<%

    // Set the flag that phrasesearch.aspx is loaded
    isFormPageLoaded = true;
    
%>
<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Internal site search for exact keyword or an exact phrase.</title>

    <!--
    /* **************************************************************
    
    Script Title: Exact Phrase Live Search
    Author: Tushar Arora
    Genre: General website exact keyword/phrase search Revision 7
    Freeware
    Released: 13-08-2010
    Version: 2.0
    Runs on website domain (www.mydomain.com), ASP (.NET Framework 2.0)
    My website with downloads: http://www.lawsofbrahman.com
    Contact me at: aroratushar@gmail.com, or comment me at my website's comments and guestbook areas.
    Read the readme.txt file for information, or contact me.    
    
    ******************************************************************/
    -->

<style type="text/css">
.style1 {
	border-style: solid;
	border-width: 2px;
	background-color: #66CCFF;
}
.style2 {
	font-size: large;
	font-family: Verdana, Geneva, Tahoma, sans-serif;
	color: #000000;
}
.style3 {
	color: #000080;
}
.style4 {
	color: #000080;
	font-size: x-small;
}
.style5 {
	font-size: x-small;
}
.style6 {
	color: #000080;
	font-size: small;
}
.style7 {
	font-size: small;
}
.style8 {
	color: #FFFF00;
	text-align: center;
	border: 1px solid #000080;
}
.style9 {
	color: #000080;
	text-align: left;
	border: 1px solid #000080;
	font-family: Verdana, Geneva, Tahoma, sans-serif;
	font-size: x-small;
	background-color: #FFCC66;
}
.style10 {
	color: #000080;
	text-align: center;
	border: 1px solid #000080;
	font-family: Verdana, Geneva, Tahoma, sans-serif;
	font-size: x-small;
	background-color: #CCFFCC;
}
.style11 {
	font-family: Verdana, Geneva, Tahoma, sans-serif;
	font-size: small;
}

</style>
</head>

<body>

<%

    // Check if the form was filled and posted to this same document?

    String ReplyFormFlag = "";
    Boolean FormSearchPosted = false;
    String txtphraseString = "";
    String selnumresultsString = "";
    int inumresultslimit = 0;
    String selnumfilessearchedString = "";
    int inumfilessearchedlimit = 0;
    
    // Check if Form was posted to this same document before    
    ReplyFormFlag = Request.Form["ReplyForm"]; // Request.QueryString["ReplyForm"];
    if (ReplyFormFlag == "1")
    {
        // Form was posted
        FormSearchPosted = true;
    }
    else
    {
        FormSearchPosted = false;
    }

    // Based on if the form was posted, create the entire webpage structure, whether
    // search material, or the general search engine form.
    if (FormSearchPosted == true)
    {
        // Form was posted so extract the form data
        txtphraseString = Request.Form["txtphrase"];

        // Get the user selected results search limit variable
        selnumresultsString = Request.Form["selnumresults"]; 
        switch(selnumresultsString)
            {
            case "search10":
                inumresultslimit = 10;
                break;
            case "search50":
                inumresultslimit = 50;
                break;
            case "search100":
                inumresultslimit = 100;
                break;
            case "search500":
                inumresultslimit = 500;
                break;
            case "Searchunlimited":
                inumresultslimit = -1; //100000; // By default 1 lakh results are the limit for the unlimited option
                break;
            default:
                inumresultslimit = 50;
                break;
            }

        // Get the user selected files searched counter limit variable
        selnumfilessearchedString = Request.Form["selnumfilessearched"];
            switch(selnumfilessearchedString)
            {
            case "search10":
                inumfilessearchedlimit = 10;
                break;
            case "search50":
                inumfilessearchedlimit = 50;
                break;
            case "search100":
                inumfilessearchedlimit = 100;
                break;
            case "search500":
                inumfilessearchedlimit = 500;
                break;
            case "search1000":
                inumfilessearchedlimit = 1000;
                break;
            case "Searchunlimited":
                inumfilessearchedlimit = 100000; // By default 1 lakh files are the limit for the unlimited option
                break;
            case "SearchCustom":
                inumfilessearchedlimit = myfileslimit;
                break;
            default:
                inumfilessearchedlimit = 50;
                break;
            }

        
        
    // Create the header template along with the simple search engine
    DisplayPrimarySearchForm();


    Response.Write("<!-- The header of the search engine only displayed when it is searching... -->" +
    "<hr />" +
    "<div style=\"background-color:#FFCC66; border:medium; border-style:solid; border-color:green;\">" +
    "<p class=\"style2\">Search result(s) for:- &#39;<em><b style=\"color:white;\">" + txtphraseString + "</b></em>&#39; are displayed below:" +
    "<br /></p>" +
    "</div>" +
    "<hr />");
%>        

    <!-- From here I shall program the live searching of the keyword or an exact phrase and
        display it live here one by one...
    -->

<% 

        // Check if the valid amount of characters are passed in the post.
        if (txtphraseString.Length <= 2)
        {

            // User did not supplied with at least 3 characters so abort
            Response.Write("<p><b>Please specify a word or a phrase with more than 2 characters in length. Aborting search...</b></p>");
            goto GlobalEndSearching;            
        }

        // Check if valid characters are passed in the post.
        if (!IsValidSearchPhrase(txtphraseString))
        {
            // User did not supplied a valid string
            Response.Write("<p><b>Only provide a valid keyword/phrase which does not consists of non-ascii, raw, hex, and other invalid characters, and only single line is accepted. Please input alphabets, space, and digits only. Aborting search...</b></p>");
            goto GlobalEndSearching;            
            
        }
        
        // Check any compromise/eves dropping to the website's security and abort if necessary 
        if ((txtphraseString.IndexOf("admin",0,txtphraseString.Length,StringComparison.CurrentCultureIgnoreCase) >= 0) ||
            (txtphraseString.IndexOf("password",0,txtphraseString.Length,StringComparison.CurrentCultureIgnoreCase) >= 0) ||
            (txtphraseString.IndexOf("localhost",0,txtphraseString.Length,StringComparison.CurrentCultureIgnoreCase) >= 0) ||
            (txtphraseString.IndexOf("administrator",0,txtphraseString.Length,StringComparison.CurrentCultureIgnoreCase) >= 0) ||
            (txtphraseString.IndexOf("passwd",0,txtphraseString.Length,StringComparison.CurrentCultureIgnoreCase) >= 0) ||
            (txtphraseString.IndexOf("phrasesearch.aspx.cs",0,txtphraseString.Length,StringComparison.CurrentCultureIgnoreCase) >= 0) ||
            (txtphraseString.IndexOf("phrasesearch.aspx",0,txtphraseString.Length,StringComparison.CurrentCultureIgnoreCase) >= 0) ||
            (txtphraseString.IndexOf("root",0,txtphraseString.Length,StringComparison.CurrentCultureIgnoreCase) >= 0))
        {
            Response.Write("<div style=\"background-color:navy;border:thin;border-style:solid;\">" +
            	"<b style=\"color:white;\">You are not allowed to search words which compromise the security of the " +
	            "domain and the server. Aborted.<br /></b></div>");
            goto GlobalEndSearching;
   
        }

        // Now finally begin the search in all directories and valid files for the exact word or the exact phrase...
        Response.Write("<em>Searching in .htm, .html, .txt, .nfo, .rtf files...</em><br />");
        if (cPhraseSearch.SearchForPhraseInFiles(txtphraseString, inumresultslimit, inumfilessearchedlimit) == true)
        {
            // Processed file(s) and directory(s)   
        }
        else
        {
            // Did not processed file(s)...   
        }
        
        // Write the footer finally.
%>    
    <hr />
    <div style="background-color:#FFCC66; border:medium; border-style:solid; border-color:green;">
    <p class="style2">Search completed, found (<b><% =cPhraseSearch.numMatchingFilesFound %></b>) files with match(s).
    <br />
    </p>
    </div>

    
<%
    }   // Finished generating of the structure containing header, searched items, and footer.
    else
    {
        // Form was not posted so create the general search engine without doing primary activity...    
%>

<!-- Header of the general search engine page -->

<p class="style11" style="width: 539px; height: 48px">
<strong>Live site search:- Search exact words and exact phrases in all the listed internal webpages of this website. Enter upto 400 characters of a keyword or a phrase, exclude line breaks.
</strong>
</p>
<hr />

<%
        
        
        
    // ********************************************************************
        
    // This is the general area where form will be usually displayed without any searching

    // ********************************************************************
                    
    // Common Search Engine form displayed as primary page.

    // Create the search engine form
    DisplayPrimarySearchForm();


    } // End here the general form body        

    GlobalEndSearching:
            
%>

</body>

</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions