Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Welcome to the TechnoWorldInc! Community!
Recent Updates
[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » PHP
  Make a search engine for your website with PHP
Pages: [1]   Go Down
  Print  
Author Topic: Make a search engine for your website with PHP  (Read 1005 times)
Daniel Franklin
TWI Hero
**********


Karma: 3
Offline Offline

Posts: 16647


View Profile Email
Make a search engine for your website with PHP
« Posted: September 26, 2007, 11:49:00 AM »


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; charset=gb2312">

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

}

TO

foreach($array as $value){

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

echo "$value"."
\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,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 "$title "."
\n";

} ?>

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

Articles Source - Free Articles

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.137 seconds with 24 queries.