Set up site search powered by Google

This simple tutorial allows you to set up a search form on your site powered by Google. Here’s how to do it.

1. Save a file called search.php on the root of your website with the following code:

Code:
<?php
$query=$_REQUEST["query"];
$query="site:{$_SERVER["SERVER_NAME"]} ".$query;

header("Location: http://www.google.com/search?q=$query");
?>

2. Paste this code on your site wherever you want the search form:

Code:
<form action="/search.php" method="post">
Search: <input type="text" name="query"><br>
<input type="submit" value="Search">
</form>

Done! That’s all there is to it. Basically what happens is your search form sends the search phrase to the first script (search.php), which adds your domain name on the beginning, then does a search on Google. So if a user typed “honey badger” into the search box, the script would send them to a Google search for “site:yoursite.com honey badger”. This tells Google to search only “yoursite.com” for the given term. Of course, for this to work, Google has to have indexed your site already. If you are not sure if this is done, just do a search for “site:yoursite.com” (replacing yoursite.com with your real domain name). If you get results, then you are in the index.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s