1. create a search.htm in the root directory of the website with the following Content: htmlheadtitle search form/titlemetahttp-equiv = "content-Type" Content = "text/html; charset = gb2312"/headbodybgcolor = "# fffftex"
1. design search forms
Create a search.htm file under the root directory of the website. The content is as follows:
Search form
2. search programs
Create a search. php file under the root directory to process the data transmitted from the search.htm form. the content is as follows:
// Obtain key words for search
$ Keyword = trim ($ _ POST ["keyword"]);
// Check whether it is empty
If ($ keyword = ""){
Echo "the keyword you want to search for cannot be blank ";
Exit; // end the program
}
?>
In this way, if the keyword entered by the visitor is empty, a prompt can be prompted. The following shows how to traverse all objects.
We can use the progressive method to traverse all files. we can use the opendir, readdir, and PHP Directory functions. We now use the former.
// Function for traversing all objects
Function listFiles ($ dir ){
$ Handle = opendir ($ dir );
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = '.' & $ File! = '..'){
// Continuous search for directories
If (is_dir ('$ dir/$ file ')){
ListFiles ('$ dir/$ file ');
}
Else {
// Process it here
}
}
}
}
?>
In the red text, we can read and process the searched files. the following describes how to read the file content and check whether the content contains the keyword $ keyword. if the content contains the keyword $ keyword, assign the file address to an array.
// $ Dir is the search directory, $ keyword is the key word for search, and $ array is the storage array
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 the file content
$ Data = fread (fopen ('$ dir/$ file', 'r'), filesize (' $ dir/$ file '));
// Do not search for itself
If ($ file! = "Search. php "){
// Match
If (eregi ('$ keyword', $ data )){
$ Array [] = '$ dir/$ file ';
}
}
}
}
}
}
// Define an array $ array
$ Array = array ();
// Perform the function
ListFiles ('.', 'php', $ array );
// Print search results
Foreach ($ array as $ value ){
Echo '$ value '.'
';
}
?>
Now we can combine this result with a program at the beginning and enter a keyword. then we will find all the relevant results on your website. We are now improving it.