PHP Research And Development

Sharing PHP Script, PHP News, Download web application, web tutorial, Learning PHP and more

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 substantial
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:

<html>

<head>

<title>Web Search</title>

<meta http-equiv="Content-Type" content="text/html">

</head>

<body bgcolor="#FFFFFF" text="#000000">

<form name="form1" method="post" action="search.php">

<table width="100%" cellspacing="0" cellpadding="0">

<tr>

<td width="36%">

<div align="center">

<input type="text" name="keyword">

</div>

</td>

<td width="64%">

<input type="submit" name="Submit" value="Search">

</td>

</tr>

</table>

</form>

</body>

</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.

<br/> &amp;lt;?php<br/> //get keywords<br/> $keyword=trim($_POST["keyword"]);<br/> //check if the keyword is empty<br/> if($keyword==""){<br/> echo"no keywords";<br/> exit; <br/> }<br/> ?&amp;gt;<br/>

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.

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

<br/> &amp;lt;?php <br/> function listFiles($dir,$keyword,&amp;amp;$array){<br/> $handle=opendir($dir);<br/> while(false!==($file=readdir($handle))){<br/> if($file!="."&amp;amp;&amp;amp;$file!=".."){<br/> if(is_dir("$dir/$file")){<br/> listFiles("$dir/$file",$keyword,$array);<br/> }<br/> else{<br/> //read file<br/> $data=fread(fopen("$dir/$file","r"),filesize("$dir/$file"));<br/> //avoid search search.php itself<br/> if($file!="search.php"){<br/> //contain keyword?<br/> if(eregi("$keyword",$data)){<br/> $array[]="$dir/$file";<br/> }<br/> }<br/> }<br/> }<br/> }<br/> }<br/> //define array $array<br/> $array=array();<br/> //execute function<br/> listFiles(".","php",$array);<br/> //echo/print search results<br/> foreach($array as $value){<br/> echo "$value"."&amp;lt;br&amp;gt;n";<br/> }<br/> ?&amp;gt;<br/>

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

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

WITH

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

CHANGE THE FOLLOWING

<br/> foreach($array as $value){<br/> echo "$value"."&amp;lt;br&amp;gt;n";<br/> }<br/>

TO

<br/> foreach($array as $value){ <br/> list($filedir,$title)=split("[ ]",$value,"2"); <br/> echo "&amp;lt;a href=$filedir&amp;gt;$value&amp;lt;/a&amp;gt;"."&amp;lt;br&amp;gt;";<br/> }<br/>

(3) Set time limit for PHP execution

ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES

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

<br/> &amp;lt;?php<br/> 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,&amp;amp;$array){<br/> $handle=opendir($dir);<br/> while(false!==($file=readdir($handle))){<br/> if($file!="."&amp;amp;&amp;amp;$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("&amp;lt;body([^&amp;gt;]+)&amp;gt;(.+)&amp;lt;/body&amp;gt;",$data,$b)){<br/> $body=strip_tags($b["2"]);<br/> else{<br/> $body=strip_tags($data);<br/> }<br/> if($file!="search.php"){<br/> if(eregi("$keyword",$body)){<br/> if(eregi("&amp;lt;title&amp;gt;(.+)&amp;lt;/title&amp;gt;",$data,$m)){<br/> $title=$m["1"];<br/> }<br/> else{<br/> $title="no title";<br/> }<br/> $array[]="$dir/$file $title";<br/> }<br/> }<br/> }<br/> }<br/> }<br/> }<br/> $array=array();<br/> listFiles(".","$keyword",$array);<br/> foreach($array as $value){<br/> list($filedir,$title)=split("[ ]",$value,"2"); <br/> echo "&amp;lt;a href=$filedir target=_blank&amp;gt;$title&amp;lt;/a&amp;gt;"."&amp;lt;br&amp;gt;";<br/> }<br/> ?&amp;gt;<br/>

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

Rory Canyons is the founder of ScriptMenu.com.
Fore more articles, scripts or tips for PHP, ASP, ASP.NET, PERL, XML,
Java, JavaScript, Flash, CFML, Python and other web programming
resources, visit http://www.scriptmenu.com

AddThis Social Bookmark Button

0 comments

Post a Comment