How to Make a Domain Search Engine With PHP

How to Make a Domain Search Engine With PHP thumbnail
PHP provides a powerful framework for building a website.

PHP is a powerful scripting language that is most often utilized by Web developers to create dynamic Web pages and Web applications. The range of functionality that PHP provides to a programmer is virtually endless, being able to create data to send to a user as well as manipulating files and database entries on the back end. Because of this, PHP is an excellent choice of language if you need to build a search engine for your website.

Instructions

    • 1

      Create the search engine's Web form. In this example, the file of the PHP script will be named "search.php." Your form will need a text input box and a "Submit" button.

      <form action=search.php method=get>

      <input name=key> <input type=submit value=Search>

      </form>

    • 2

      Create a function to search the server's files and directories. Making a function will allow easy searches of subdirectories. The function will have one required parameter, which will be the search string.

      <?

      function search($keyword){

      }

      ?>

    • 3

      Add an optional parameter for subdirectory paths. If this second parameter is not specified, the function will default to searching the current directory.

      function search($keyword){

      if(func_num_args()>1) $dir = func_get_arg(1);

      else $dir = ".";

      }

    • 4

      Read the contents of the directory into an array. Add this code to the search() function.

      $dirStream = opendir($dir);

      while($nextFile = readdir($dirStream)) $dirArray[] = $nextfile;

      closedir($dirStream);

    • 5

      Iterate through the contents of the directory array using a foreach loop.

      foreach($dirArray as $thisFile){

      }

    • 6

      Create a variable to store the relative path from the PHP script to the file being searched. Add this within the foreach loop.

      $thisPath = $dir."/".$thisFile;

    • 7

      Add a line to prevent the search engine from showing results from hidden files (such as ".htaccess").

      if(substr($thisFile,0,1) == ".") continue;

    • 8

      Check to see if the current filename in $dirArray is actually a subdirectory, in which case it will be stored in an array to search through later.

      if(filetype($thisPath)=="dir"){

      $subDir[] = $thisFile;

      continue;

      }

    • 9

      Check the contents of the current file to see if it contains the search string stored in $keyword, and, if so, display a link to the file. Your foreach loop should now look like this:

      foreach($dirArray as $thisFile){

      $thisPath = $dir."/".$thisFile;

      if(substr($thisFile,0,1) == ".") continue; // don't search hidden files, which begin with "."

      if(filetype($thisPath)=="dir"){

      $subDir[] = $thisPath; // add to an array listing subdirectories

      continue;

      }

      if(strpos(file_get_contents($thisPath),$keyword)!==false)

      echo "<a href=\"".str_replace("./","",$thisPath)."\">".$thisFile."</a>\n";

      }

    • 10

      Add a line at the end of the search() function to iterate through the $subDir array and search all subdirectories. If you do not wish to search subdirectories, don't add this line.

      foreach($subDir as $thisDir) search($keyword,$thisDir);

    • 11

      Add a final line of PHP after the complete search() function to see if form data has been submitted, and, if so, call the search() function. Your complete code should look like this:

      <form action=search.php method=get>

      <input name=key> <input type=submit value=Search>

      </form>

      <?

      function search($keyword){

      if(func_num_args()>1) $dir = func_get_arg(1);

      else $dir = ".";

      $dirStream = opendir($dir);

      while($nextFile = readdir($dirStream)) $dirArray[] = $nextFile;

      closedir($dirStream);

      foreach($dirArray as $thisFile){

      $thisPath = $dir."/".$thisFile;

      if(substr($thisFile,0,1) == ".") continue;

      if(filetype($thisPath)=="dir"){

      $subDir[] = $thisPath;

      continue;

      }

      if(strpos(file_get_contents($thisPath),$keyword)!==false)

      echo "<a href=\"".str_replace("./","",$thisPath)."\">".$thisFile."</a>\n";

      }

      foreach($subDir as $thisDir) search($keyword,$thisDir);

      }

      if(isset($_GET['key']) && $_GET['key']!="") search($_GET['key']);

      ?>

Tips & Warnings

  • You can easily add additional features to your search engine, such as conditional searches and ranked results, by making minor changes to the search() function or calling it multiple times.

  • The example is little more than a bare PHP script. Be sure to add all necessary HTML tags (such as "<doctype>" and "<body>") to make your script standards-compliant.

Related Searches:

References

Resources

  • Photo Credit building internet image by Danielle Bonardelle from Fotolia.com

Comments

You May Also Like

Related Ads

Featured