articleandcontent.com articleandcontent.com
Site Home About Us Security & Privacy ToS Place Your Link Submit Article
Search:   
Add Url
 

Art & Creative

Online & Indoor Games

Fashion & Relationships

Research & Science

Automobiles

Computers & Software

News & Media

Shopping & Auction

Government & Politics

Healthcare & Treatment

Business & Services

Sports

People & Society

Recreation & Entertainment

Hygiene & Health

Teens & Children

Family & Home

Self Enhancement

Property & Agents

Education & Learning

Tour & Travel

Banking & Finance

Jobs & Employment

Drink & Food

 

Site Home › Computers & Software › Web Development Services
 

Design a Search Engine for Your Own Site with PHP

 
Author: Rory Canyons

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.

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

} ?>

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("",$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=$filedir>$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,

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("",$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>"."";} ?>

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

Author Bio:
Rory Canyons is a famous writer. Rory likes to scribble articles about this topic.
You can search for this article using: web site development, web design & development, website development tampa
 
 
 

Related Articles

 
How to Get Started With Your Hosting Plan
 
6 Guaranteed Ways To Improve Your Traffic With Blogs
 
Business Plan Software
 
What Visitors Like and Dislike (revised)
 
Virtual Real Estate : Why You Should Know What It Is And Start Building It!
 
Local Advertising for Affiliates
 
Thinking about Free or Cheap Web Hosting? Think Again! It could cost you more than you think!
 
Are You Considering A Registry Tool Improvement?
 
Create Your Own Viral eBook in Under 30 Minutes
 
Ezine Publishing: 5 Ways to Make Sure Your Ezine Gets Delivered
 
 
 
 

Search Engine Rank: Development Considerations

Consider your desired search engine rank when developing your website. - Christopher J Enders
 

New Media and Online Marketing ? Strategies, Advantages and Disadvantages

The advent of the New Media and Internet have increased possibilities of online marketing and intern ... - Verena Veneeva
 

Hosting Your Way All The Way To The Bank

Choosing a hosting company is an important decision. Does your hosting company meet your needs? If n ... - Claudette Spedden
 
 

How To Transform Your Profits Using Direct Response and Copyrighting Secrets

How Direct Response and Copyrighting Secrets Used Off and On Line turned this Struggling Local Audio ... - Geoff Morris
 

Why Backlinks Are Important For Your Website's Search Engine Ranking

Like any investment, your website must start turning a profit as early as possible, and pulling the ... - Kalyan Kumar
 

Creating Revenue Without Much Effort, Set It And Forget It Online

Frequently updated content is king. Learn how to increase the quality of your site while driving mor ... - Andrew Hillman
 

Getting Seen on Google without Paying a Penny

With blogs and new web sites being launched in their thousands every day, it?s getting harder to get ... - Gary Paul
 

Google Adsense - The "Duplicate Content" Controversy

Many opinions abound on the forums and elsewhere on the web discussing duplicate content. And many n ... - Alden Smith
 
 
Site Home -> Security & Privacy -> ToS  
© 2006-2008 www.articleandcontent.com All Rights Reserved Worldwide.