Jump to content

Website Search Function


adrian.s85
 Share

Recommended Posts

Hi everybody,I'm trying to write a php script that performs a search for a string through my entire website.I put together a script with an example script of directory traversing found on the internet and some code I wrote to perform the search on each file. So far is working quite nice but there is one big problem.The script searches for every *.php page in my entire website (I only have php pages) and then it evaluates them so as to get the resulting html page with the appropriate content (fetched from databases) and then performs the search on each of them and returns the results with a link to the page where it found it.The problem I have is that when it evaluates the php page it does so without giving any parameters, my question is the following: is it possible with the eval() function to pass as argument a page like this "page.php?argument=value". And if it is which would be the best way to find all the possible argument combinations of one page.Thanks in advance.

Link to comment
Share on other sites

sorry guys I made a mistake in my question:I actually call the function: $content = file_get_contents("page.php"); and then: eval("?>" . $content . "<?");so the question I guess it would be if I can use the first function like this: $content = file_get_contents("page.php?argument=value");and then evaluate it.thanks again

Link to comment
Share on other sites

How about you just include the file over HTTP and then you don't need to eval it, and the querystring will work:file_get_contents('http://localhost/page.php?arg=val');That's a lot of work for a search though. Wouldn't it be easier to search through the content in the database? Or index the site periodically and then search through the index.

Edited by justsomeguy
Link to comment
Share on other sites

What is the recommended method for indexing a site? Or, for that matter, of organizing and carrying out a search? Just an algorithm, I'm not angling for code

Link to comment
Share on other sites

I haven't built an indexer or crawler, but you would need a script to load each page and record things like the title and headings and the text content, and it needs to find links to other pages on your site and follow the links to get to the next page. If it stores all of that data in the database then it's going to be a lot faster to just search through the database then it will to open each file and search through that. You only need to run the indexer again when the content changes.

Link to comment
Share on other sites

thanks for the fast reply! :)ok, i didn't think of that but i think it will work...now the problem would be trying to find each link on the page.....i think I will try by searching the "href" string in the page and then parse the link and check if it is a php page with some arguments (while those without arguments are already taken care of).I know it's a lot of work and I also know that there are some scripts out there ready to use but I'm just doing my homepage for fun and the main idea was to learn all the php/css/xhtml/mysql stuff....so as far as I can manage it I will try to do all by myself :)thank you very much :)

Link to comment
Share on other sites

actually if somebody is interested this is the script I came up (a big thanks goes to the guy that did the recursion for the directory traversing, I tried it too but I always got a strange error...)comments and tips are appreciated :)

<?php//search in files on the server$search_word = $_GET['search'];if ($search_word) {$directory = '..';try	{		//check if directory is valid		if( !is_dir($directory) )		{			throw new Exception('Directory does not exist!'."\n");		}		$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, 0));		// loop directly over the object		while($it->valid())			{			// check if value is a directory			if(!$it->isDot())			{				if(!is_writable($directory.'/'.$it->getSubPathName()))				{					//echo 'Permission Denied: '.$directory.'/'.$it->getSubPathName()."\n";				}				else				{										// get filename					$file = $directory.'/'.$it->getSubPathName();										if (stristr($file, ".php")) {						//get file contents						$contents = file($file);												//evaluate contents in a buffer						ob_start();						$content = file_get_contents($file);						$content = eval("?>".$content."<?");						echo $content;						//get buffer content						$content = ob_get_contents();						ob_end_clean();														//sanitize						$content = filter_var($content, FILTER_SANITIZE_STRING);												//search for string						if (stristr($content, $search_word)) {														//highlight results in content and show the results							$content = str_ireplace($search_word, "<span class='highlight'>" . $search_word . "</span>", $content, $count);														echo "<div class='post'>";							echo "<a class='title' href='" . $file . "?highlight=".$search_word."'>" . basename($file) . " (".$count.")</a>";							echo "<p class='text'>" . $content . "</p>";							echo "</div>";						}					}				}			}			// move to the next iteration			$it->next();		}	}	catch(Exception $e)	{		echo $e->getMessage()."\n";	}}?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...