Article Read. You Can Find All Kind of Articles

Home | Search Article

Search



Advanced Search

Make a Search Engine For Your Website With PHP

Kategori  Category : Web Development
Read  Times Read : 53
Date  Date : 13 March 2008 16:15

 by: Rory Canyon

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, lets 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.

<?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 lets go through all the files or articles in your website.

<?php

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.

<?php

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"."<br>";

}

?>

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>(.+)

</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"."<br>";

}

TO

foreach($array as $value){

list($filedir,$title)=split("[ ]",$value,"2");

echo "<a href=$filedir>$value</a>"."<br>";

}

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&#65292;so ten minutes is the litmit.

Now, combine all the above programs and get the complete search.php file as following,

<?php

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("<body([^>]+)>(.+)</body>",$data,$b)){

$body=strip_tags($b["2"]);

}

else{

$body=strip_tags($data);

}

if($file!="search.php"){

if(eregi("$keyword",$body)){

if(eregi("<title>(.+)</title>",$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 "<a href=$filedir target=_blank>$title </a>"."<br>";

}

?>

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

 

Web Development

Most Popular Articles

Random Article 1

Random Article 2

  • YOUR FIRST HTML PAGE - IV
  • In the preceding sections, you learnt how to come up with an elementary HTML page. You learnt the tags that are the backbone of an average HTML page, namely, , , , ,

    and .Assuming you could assimilate the gushing fountain of wisdom in the previous articles, we move onto the streams of more evolv

  • Web Accessibility and Your Business
  • One of the things I find Im most often fighting with my peers and competitors is the need for your website to be accessible. An Accessible website is a website that caters to the needs of disabled individuals and people using alternative web browsing methods like third party browsers, PDAs, etc. As

Random Article 3

Random Article 4

  • Testing and Tracking Your Online Marketing Efforts
  • Most advertisers and marketers, because they dont test and track the response of various marketing activities, are flying blindly, with little or no idea of whats working - and what isnt. Proper testing and tracking can give you the most accurate and reliable answers to virtually any marketing quest
  • How to choose the right domain name for your company
  • As the editor of DomainNameWire.com, Im frequently e-mailed by people asking for domain name advice. Specifically, what should they look for when registering a domain name for their company? This may be basic for some of you, but I hope some of my readers gain value from this.Let me start with the t
indir notebook hiperucuz.com teknoloji forum hava kargo adtech ile reklam 2.0 dönemi ba?l?yor ve Trkycmhrytllbtpydrklcktr r10.net seo yar??mas?