Auto-Populate Site Search with Terms.

Published in: Web Development.

I just designed and built a new site for a company who is shifting their online strategy from e-commerce to providing relevant content to their customers. One of the problems upon switching, however, was broken links. All the incoming links were bad: referrals, organic searches, everything. Unless your link brought you to the home page, you got a 404 error.

I already had a custom 404 page set up to match the site’s design and help explain that this is a new site, so try searching, or use the navigation to browse the site. Below that I had a search box.

But I could make this one step better for user’s arriving via search engines by auto-populating the search box with the terms used on the search engine to get there. So a user can search the entire site for their keywords with one click.

Here’s the code:


// Gets the referring URL and loads it into $referURL
if(isset($_SERVER['HTTP_REFERER'])) {
  $referURL = $_SERVER['HTTP_REFERER'];
	
  // Divide the referring URL by slashes, providing the source and search string
  $referURL = explode  ( "/", $referURL );
  if(count($referURL) >= 4) {
    $source = $referURL[2];
    $searchString = $referURL[3];
		
    // Evaluate the search string depending on the referring browser
    switch($source) {
      case "www.google.com":
        $query = extract_searchTerms($searchString, "q=", "+");
        break;
      case "www.bing.com":
        $query = extract_searchTerms($searchString, "q=", "+");
        break;
      case "search.yahoo.com":
        $query = extract_searchTerms($searchString, "p=", "+");
        break;
    }
  }
}

And here’s the extract_searchTerms function:


function extract_searchTerms(&$searchString, $searchParam, $searchDelimiter) {
  // Remove everything before the URL search parameter
  $searchString = strstr($searchString, $searchParam); 
  // Remove the search parameter and everything after the "&"
  $searchString = substr($searchString, strlen($searchParam), strpos($searchString, '&') - strlen($searchParam));
  // Separate the Search Keywords by the search delimiter
  $searchString = explode($searchDelimiter, $searchString); 
  // Reunite the Search Keywords, separated by a space
  $searchString = implode(" ", $searchString); 
  return $searchString;
}

The last step is to load the $query variable as the Search Box value:


<!-- Call the created PHP variable for the Value attribute -->
<input class="text-input" type="text" value="<?php echo($query); ?>" />

So that’s it for Google, Bing, and Yahoo. To add another search engine, you just need to figure out how they’re denoting the keywords in the http referrer.

What do you think? Good practice? Too much work for too little return?

You could even take this a step further and redirect a user to the site’s search results instead of serving an error page at all. I’m afraid that takes too much control away from the user and might cause some confusion.

    Share:

    Permalink:
    https://writings.orangegnome.com/writes/auto-populate-site-with-search-terms/

    Pingbacks:

    Comments are disabled on this blog. If you have a thought, contact me on Twitter or on the contact form. If you have a really long thought, send me a link to your post about it.