Design a Search Engine for Your Own Site with PHP

0


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:

<title> Web Search </ title> <meta http-equiv = Content-Type content = text / html>

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. <? php / get keywords $ keyword = 'trim ($ _ POST ["keyword"]);' // check if the keyword is empty if ($ keyword = '= "" {{echo "no keywords"; exit; }?>

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.

<? php function listFiles ($ dir) {$ handle = opendir ($ dir); while (false! == ($ file = readir ($ handle))) {if ($ file! =. && $ file! = ..) {// if it is a directory, then continue if (is_dir ("$ dir / $ file ")) {listFiles (" $ dir / $ file "); } else {// process the searching here with the following PHP script}}}}?>
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.

<? php function listFiles ($ dir, $ keyword, & $ array) {$ handle = opendir ($ dir); while (false! == ($ file = readir ($ handle))) {if ($ file! =. && $ file! = ..) {if (is_dir ("$ dir / $ file")) {listFiles "$ dir / $ file", $ keyword, $ array); } else {// read file $ data = 'fread (fopen ("$ dir / $ file", "r"), filesize ("$ dir / $ file"))); // avoid search search.php itself if ($ file! = search.php) {// contain keyword? if (eregi ("$ keyword", $ data)) {$ array [] = $ dir / $ file; }}}}}} // define>

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

if (eregi ("$ keyword", $ data)) {$ array [] = "$ dir / $ file"; }

WITH

if (eregi ("$ keyword", $ data)) {if (eregi ("<title> (. + (</ title>", $ data, $ m)) {$ title = $ m ["1"] ; } else {$ title = "no title"; } $ array [] = "$ dir / $ file $ title"; } (2) Add links to searching results

CHANGE THE FOLLOWING

foreach ($ array as $ value) {echo "$ value". "n"; }

TO

foreach ($ array as $ value) {list ($ filedir, $ title) = split ("[]", $ value, "2"); echo "<a href = https: //translate.googleusercontent.com/translate_c? depth = 1 & amp; hl = en & amp; ie = UTF8 & amp; prev = _t & amp; rurl = translate.google.com & amp; sl = en & amp; sp = nmt4 & amp; tl = en & amp; u = http: //ezinearticles.com/%2524filedir& usg = ALkJrhifgmy9YfV0jVrK2vEAlkOV4RA0mg> $ value </a>. "" "; } (3) Set time limit for PHP execution

ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES

set_time_limit ("600");
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,

<? php set_time_limit ("600"); <br> $ keyword = trim ($ _ POST ["keyword"]); <br> if ($ keyword == "") {<br> echo "Please enter your keyword"; <br> exit; <br>} <br> function listFiles ($ dir, $ keyword, & $ array) {<br> $ handle = opendir ($ dir); <br> while (false! == ($ file = readdir ($ handle))) {<br> if ($ file! = "." && $ file! = "..") {<br> if (is_dir ("$ dir / $ file")) {<br> listFiles ("$ dir / $ file", $ keyword, $ array); <br>} <br> else {<br> $ data = fread (fopen ("$ dir / $ file", "r"), filesize ("$ dir / $ file")); <br> if (eregi ("<body ([^>] +) & gt; (. +)

", $ 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

Leave A Reply
Bitcoin (BTC) RM308,586.10
Ethereum (ETH) RM15,129.69
Tether (USDT) RM4.78
BNB (BNB) RM2,943.84
USDC (USDC) RM4.78
XRP (XRP) RM2.53
BUSD (BUSD) RM4.79
Cardano (ADA) RM2.26
Solana (SOL) RM705.83
Dogecoin (DOGE) RM0.727728
Polkadot (DOT) RM33.16
Polygon (MATIC) RM3.45
Lido Staked Ether (STETH) RM15,125.54
Shiba Inu (SHIB) RM0.000124
Dai (DAI) RM4.77
TRON (TRX) RM0.559448
Avalanche (AVAX) RM171.40