Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Your Email:

PHP Site Search Made Easy

By Akash Mehta
2008-03-30


Building a real site search system

So, we've built a simple site search system with a form and a raw call to the web services. However, we have no validation, and our results aren't exactly pretty. We also want the user to be able to search from elsewhere in our site, without having to go to a special search page.

First, let's tackle the search system. We need to:

  • Make it easy to configure which site we want to search (i.e. ours)
  • Validate that the user actually entered something
  • Make sure no errors were encountered in the web search call
  • Return more than 10 results

The first will be easy - we just add a configuration variable.

The second is also fairly simple: where we used isset($_GET['q']), we can add AND !empty($_GET['q']), which will make sure that not only did the user submit the form, they also entered something into the search box.

The third, however, is a little tricky - Yahoo's standard error system doesn't really work for PHP output. Instead, it just gives us the text "Array", which not only will fail in unserialize(), but is also rather unhelpful when working out what went wrong. The services do provide HTTP error codes in the headers - such as 403 for forbidden - but working with these with only file_get_contents isn't an option. The simplest way to get around this would be to suppress errors, and then check that a single item in the result set actually exists - we can use isset() on item 0.

Finally, to return more than 10 results, we simply add back the results parameter I mentioned earlier. This simply states how many results to return. The service also has a system for paging through results to find what the user is looking for, but unless you have thousands of pages you probably won't want to use this. Let's stick to 20 results, which should be more than enough for most sites.

Incorporating these modifications, here's the code (changes in bold):

<form action="" method="get">
<input type="text" name="q" /><input type="submit" />
</form>
<?php
$site = 'engadget.com';
if (isset($_GET['q']) AND !empty($_GET['q'])) {
$q = $_GET['q'];
$data = file_get_contents('http://search.yahooapis.com/'.
'WebSearchService/V1/webSearch'.
'?appid=YahooDemo&query='.$q.
'&output=php&site='.$site.
'&results=20');
$results = @unserialize($data); // @ = suppress errors
if (isset($results['ResultSet']['Result'][0])) {
foreach ($results['ResultSet']['Result'] as $result) {
echo "<h3><a href=\"{$result['ClickUrl']}\">{$result['Title']}</a></h3>\n";
echo "<p>{$result['Summary']}</p>\n";
}
} else {
echo "<p>No results were found.</p>";
}
}

Save it as search.php and load it up in your web browser. Try searching for nothing; the script will simply show you the search box. Try searching for some random collection of characters - I chose 'dkrkwrialkc' - it should tell you that no results were found. Now, we just need to make it a little more flexible.

Search from far and wide

When your users want to search, they won't look around your page for the link to a search page - they'll look for a search box outright, and they want to see one. Just about every major online portal has a search box on every page to help the user find their way around the site. We need to do the same.

Luckily, our search system is extendible out of the box. It doesn't check where the search is coming from, it just searches, so we can send the user to the search page from anywhere. And the best way to do that is to simply copy and paste the form. This HTML at the top of the script is the key:

<form action="" method="get">
<input type="text" name="q" /><input type="submit" />
</form>

The action property is the vital element. Wherever we put the script, we simply make sure the action property points to the location of search.php. For example, if I put search.php in the root of my website - i.e. accessible at http://mydomain.com/search.php - I can set action to /search.php and copy the form to any page under mydomain.com. If that isn't an option, consider using mod_rewrite to make search.php in your root point to the right location, as this is the easiest option by far for the action property.

Once that's taken care of, copy and paste the HTML for your form anywhere into your site. The top of a sidebar is a good choice, but so is somewhere in your header, or in a seperate section between your header and main content. As long as it's "above the fold", or visible without scrolling, your users will make liberal use of the feature to navigate your site.



Tutorial Pages:
» Why site search?
» A brief crash course on search APIs
» Building a real site search system
» Further reading


Related Tutorials:
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK
» Installing PHP on Windows