Your Ad Here

 Navigation
Home
Source code
Tutorials
RSS feeds
Articles
Wordpress plugins
Books
Sample Chapters
Software
Downloads
Hosting
manuals
Script directory
Training
Our Links




 Sponsors
 Links
programmershelp
low cost magazines
finance products
 Downloads
dzphp editor
EasyPHP
mysql_phpgenerator
php-studio-trial
phpdesigner_7_2_5
phpMyAdmin-3.4.3.2
/phptriad2-2-1
phpxedit_321
rapidphp 2011
WampServer2.1d-x64
WampServer2.1e-x32
xampp-win32-1.7.4-VC6-installer
xampplite-win32-1.7.3
 Misc
Webmaster Resources
Only PHP
ScriptSearch.com
Scripts.com - Get the best scripts NOW!
AndreaPHP Programming

Valid XHTML 1.0 Transitional


Valid CSS!


Make a Search Engine For Your Website 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.

<?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=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>\n";

}

?>

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>\n";

}

TO

foreach($array as $value){

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

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

}

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>\n";

}

?>

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

Written by: Rory Canyon

 



Here is our complete list of articles


Apache MySQL and PHP for Windows

Apache MySQL PHP for Windows

ASP CGI and PHP Scripts and Record Locking What Every Webmaster Needs To Know

Autoresponders With PHP

Bring Your Web Site to Life With PHP

Clickbank Security Using PHP

Create a Simple Effective PHP Form for Your Web Site

Creating Dynamic Website Content with PHP MySQL

Developing a Login System with PHP and MySQL

Developing State enabled Applications With PHP

For Automated Sites PHP and MySQL are A Perfect Match

Get PHP pages indexed in the Search engines

Getting your Visitors Details Using PHP

How PHP Can Help Save You Time And Mistakes

How to make a simple form mailer with PHP

HTACCESS Wrappers with PHP

Maguma has integrated support for the eBay Acellerator Toolkit for PHP PHP AT in their PHP IDE Maguma Workbench

Mastering Regular Expressions in PHP

More Autoresponders With PHP

MySQL Database Handling in PHP

ONLINE the eBay Accelerator Toolkit for PHP PHP AT for the PHP IDE Maguma Workbench is available

Password Protection and File Inclusion With PHP

Password Protection with PHP MySQL and Session Variables

PHP Account Activation

PHP and Cookies a Good Mix

PHP Auto surf Websites

PHP Databases

PHP Dynamic Content

PHP Email

PHP Encryption

PHP Error Pages

PHP Files

PHP Form Series Part 1 Validators Client side Validation

PHP General Scripting

PHP Image Gallery

PHP in the Command Line

PHP is 10 years old

PHP my favorite Server side Programming Technology

PHP On The Fly

PHP Pear Packages Why they are so important to php developers

PHP Redirect

PHP Scripts Dont Have to End in PHP

PHP Server to Client with No Refresh

PHP Sessions / Cookies

PHP Templates

PHP User Login Authentication

PIM Team Case Study Creating Text Effects With PHP and GD

Protecting your HTML and PHP Source Code

Quick Intro to PHP Development

Serialize this Saving Objects in PHP

Simple Solution for Php Includes IFrames

Site Personalization With PHP

Some PHP functions you must know

Track Your Visitors Using PHP

Victoryvisions The PHP/Mysql Company Complete website development company



Books
 Sponsors


 Random Code
Counter example(application)
Server info(functions)
Latest stock market information(internet)
 Random Article
  Network
Programming resources
Tutorials directory
Hosting resources
ASP site
Domain names
Progged
Maxi directory
bigarticle : free articles
A Code
Code N Tutorials
Get PHP
Programmers help




beginners PHP Copyright © 2004 onwards by beginnersPHP.