Design a Search Engine for Your Own Site with PHP


This hands on PHP Programming article provides the knowledge necessary to design and develop a search engine for your website using PHP version 4.0 and above. Making a search engine for your website with PHP is really easy and provides substantive functionality required by most of the small to medium websites. This article introduces every steps of the development, including both design and PHP programming. Basic computer skills and knowledge of HTML fundamentals are required. Ok, let's begin now.

Step 1: Design Search Box

Under your website root, make a file called search.htm or anything you like and type in the following code:

Step 2: Write search.php file. It is the core of your website search engine. Under your website root, create a file called search.php or anything you like.

With above, you can give hints to your users when they forget to enter a keyword. Now let's go through all the files or articles in your website.


The following scripts read, process files and check wherever the files contain $ keyword. If $ keyword is found in the file, the file address will be saved in an array-type variable.

Now, combine the codes listed above, all the related results in your websites will be found and listed.

Step 3: further improvement of the search engine can be made by adding the following,

(1) list the title of all searching results

REPLACE THE FOLLOWING

WITH

(2) Add links to searching results

CHANGE THE FOLLOWING

TO

(3) Set time limit for PHP execution

ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES


The above time unit is second. 10 minutes is the script execution litmit.

Combine all the above codes and get the complete search.php file as following,

", $ data, $ b)) {
$ body = strip_tags ($ b ["2"]);
else {
$ body = strip_tags ($ data);
}
if ($ file! = "search.php") {
if (eregi ("$ keyword", $ body)) {
if (eregi ("

", $ data, $ m)) {
$ title = $ m ["1"];
}
else {
$ title = "no title";
}
$ array [] = "$ dir / $ file $ title";
}
}
}
}
}
}
$ array = array ();
listFiles (".", "$ keyword", $ array);
foreach ($ array as $ value) {
list ($ filedir, $ title) = split ("[]", $ value, "2");
echo " $ title ". "
";
}
?>

Now, you have made a search engine for your website, enjoy it!

Source by Rory Canyons

Comments (0)
Add Comment