Active Articles: 2038
Total Categories: 16
Sub Categories:616
![]() |
|
Make a Search Engine For Your Website With PHP |
|
| Date Added: July 05, 2004 11:59:38 PM | |
| Author: Rory Canyon | |
| Category: Tips and Tutorials: PHP Tips and Tutorials | |
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. //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. function listFiles($dir){ $handle=opendir($dir); while(false!==($file=readdir($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 whether the files contain $keyword. If $keyword is found in the file, the file address will be saved in an array-type variable. function listFiles($dir,$keyword,&$array){ $handle=opendir($dir); while(false!==($file=readdir($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 array $array $array=array(); //execute function listFiles(""."",""php"",$array); //echo/print search results foreach($array as $value){ echo ""$value""."" ""; } ?> Now, combine the programs listed above, you will find all the related results in your websites will be found and listed. A further optimization of the search engine can be taken 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=$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""."" ""; } TO foreach($array as $value){ list($filedir,$title)=split(""[ ]"",$value,""2""); echo ""$value""."" ""; } 3 Set time limit for PHP execution ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES set_time_limit(""600""); The above unit is second,so ten minutes is the litmit. Now, combine all the above programs and get the complete search.php file as following, set_time_limit(""600""); $keyword=trim($_POST[""keyword""]); if($keyword==""""){ echo""Please enter your keyword""; exit; } function listFiles($dir,$keyword,&$array){ $handle=opendir($dir); while(false!==($file=readdir($handle))){ if($file!="".""&&$file!=""..""){ if(is_dir(""$dir/$file"")){ listFiles(""$dir/$file"",$keyword,$array); } else{ $data=fread(fopen(""$dir/$file"",""r""),filesize(""$dir/$file"")); if(eregi(""]+)>(.+)"",$data,$b)){ $body=strip_tags($b[""2""]); } else{ $body=strip_tags($data); } if($file!=""search.php""){ if(eregi(""$keyword"",$body)){ if(eregi("" $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! About The Author Rory Canyon is the founder of ScriptMenu.com, a free web directory for PHP, ASP, ASP.NET, PERL, XML, Java, JavaScript, Flash, CFML, Python and other web programming resources. For more information, visit http://www.scriptmenu.com RELATED ARTICLES & TUTORIALS
Everyone has heard that content is king when it comes to search engine optimization and just good old plain web site marketing smarts. But how you present that content on your site can also make a world of difference. Get PHP pages indexed in the Search engines A simple method to convert files from Php to Html Extensions, on an Apache Server PHP On-The-Fly! PHP can be used for a lot of different things, and is one of the most powerful scripting languages available on the web. Not to mention it's extremely cheap and widely used. However, one thing that PHP is lacking, and in fact most scripting languages are, PIM Team Case Study: Creating Text Effects With PHP and GD See how you can create graphic effects on text with PHP and GD - drop shadows, arcs, fonts and colors. Clickbank Security Using PHP Here's a way to protect the products you sell with Clickbank, using their built-in protection and by implementing a 30-day expiration, all without having to worry about managing databases or customer lists. |
|
|
|
|
|
The views and opinions of authors expressed on Zeronese Webmaster Article Directory web sites do not necessarily state or reflect those of the Zeronese.net . |
|

