How to Create a Search Engine Using PHP 


               


How to Create a Search Engine Using PHP



PHP




You can easily create your own search engine in PHP.

This is a text box where a user types in a term and presses the submit button and then is redirected by the search engine to the page he is looking for. For example, a user may type in the phrase, 'youtube', and then be redirected to the youtube homepage.

This type of search engine isn't google. It doesn't search through dozens of sites and then sends the most relevant results based on the query. It isn't that advanced. What it does is, according to the search query that you enter, if there is a matching if statement for that query, it will redirect a person to the page most relevant according to that query. So it isn't a search engine that returns many relevant results, but a search engine that redirects a user to a page. It's a much more simpler search engine, but still very effective for basic purposes and something you may desire for your website.

Building this type of search engine is actually not hard at all. It's actually very simple code.

To do this, let's start with the HTML. We create a text box, as is shown below:

Search Engine

Search


The only terms this search engine contains so far:
Dropbox
Amazon
google
ebay
wikipedia
youtube
hess toy truck
LM7805



HTML Code

The HTML Code to create this text box above is:



Now that we've created a text box and a user is able to type in the term he or she is searching for, we now need to redirect the user to the necessary page based on the search query s/he has entered. For example, if the user enters the search query, "wikipedia", we then need to redirect him to the wikipedia website. To do this, we use PHP. PHP will extract the search term that the user has entered and use a series of if statements to see if that term matches a term we have in our website. If it does, we then direct the user to the necessary page on that search term using the PHP header function. This will be explained below. If no search term matches any that we have on our website, then we redirect the user to a page that states, "No Searches Matched".

Example

An example of this is a user looking up the term 'Hess toy trucks'. We need to create the page Hess toy trucks so that when the user enters the term, we have the page to redirect them to, and we need to create an if statement on the PHP page, searchpage.php to check which term is selected.

This is what the PHP page, searchpage.php, will look like:


Basically, the PHP page, searchpage.php, retrieves the search term that the user enters using the superglobal array, $_POST, and then compares it against a series of if statements. If the search term that the user entered matches a search term that is the series of if statements, the user is redirected to that page that the URL specifies in the header function. If none of the search terms match, the statement, No Searches Matched will be output to the screen.

one trick that we use to simplify this process is when we retrieve the search term the user entered, we lowercase the string using the strtolower() function. This why no matter what string the user enters, it becomes lowercase. Therefore, when we are comparing strings, we can just compare it only with the string with lower case alphabetical characters. An example is different users typing in "Hess Truck", "HESS TRUCK", "Hess truck", and "HESS truck". If we didn't convert these to lowercase, we would have to have comparisons to each capitalized of each letter in each word. These would be tedious. By converting the string to all lowercase, all we must do is compare the string to its all lowercase equivalent. Therefore, all we must do is compare it with the line if ($result=="hess truck").

You can put as many terms as you want in the searchpage.php and redirect users to the desired page using the header function.

This search engine works great, but there's one problem. It only returns a result if the user enters the exact phrase you are looking for. For example, if a user is trying to go find information about the LM7805 voltage regulator and types in 'LM7805 voltage regulator' instead of just 'LM7805', the search won't redirect to the page on LM7805 voltage regulators. This is because according to the code the if statement looks for the exact equal of the phrase (==). Thus, it must be a perfect match. Same thing for another search query. If a user types in 'google homepage' and not 'google', the person won't be brought to the google website. This is because again the if statement looks for a direct equal (==). So to counter, if the phrase contains the word google, such as 'google homepage' or 'google search engine', then the user will still be redirected to the google homepage, because his/her search contains the word 'google'. The same thing if the user types in 'LM7805 voltage regulator'. Because the search query contains the phrase 'LM7805', the search engine recognizes this and reroutes the person to the LM7805 voltage regulator page.

So how is this done? It's simple. Instead of the php if statement being a direct equal (==), as in, if ($result=="google"), which must be directly equal, we change the statement to, if (strpos($result, "google") == "true")

What this statement does is it looks for the phrase, 'google', in the search query. If the position of that phrase is present in the string, the if statement is true; if it is true, we can redirect them to the google homepage. This is because as long as 'google' is present in the search query, the if statement is true. And then underneath we put the header location redirect function.

So we create a new searchpage named searchpage2.php and change the if statements, as shown above, we'll get this file, which is shown below.



Now we have a search engine that doesn't require a direct character-for-character match. If the user types in 'youtube homepage' or 'youtube videos', s/he will still be redirected to the youtube homepage, because the search query contains the word 'youtube'.

And you can keep refining this search engine. For example, if a user types in 'google maps' instead of just rerouting them to the google hompage, you can create another if statement that if the result contains 'google' and 'maps' to redirect the user to google maps instead of simply the google homepage. If you do this where you're using the same root phrase 'google' but for different categories within google, it is best to use if else statements instead of just if statements. But it isn't absolutely necessary if you're using and statements. This if statement would look like this, if ((strpos($result, "google") == "true") && (strpos($result, "maps") == "true"))

Then underneath you would put the header location redirect function to, header ('Location: https://maps.google.com/');

And this is how you can build a search engine that can become more and more advanced, or more and more intelligent. You keep making more divisions and if statements. So if a user types in 'google' and 'finance' within the same search, you can bring them to the google finance page. If a user types in 'google' and 'images' in the same search, you can bring them to the 'google images' page. And so on and so forth.

A few points to make about this creating the searchpage.php page. When you do so, make sure there is absolutely no space before the <?php tag. Putting space in between the PHP tag may send output to the browser and the code will not work. Before PHP can execute this redirect function, no output can be written to the browser. No nothing at all can go before the <?php tag, not even white space. Otherwise, you will get an error that says something to the effect of, Warning: Cannot modify header information - headers already sent by (output started at /home/content/.../searchpage.php:1). This means that before PHP performed the redirect, output was already written to the browser. With the PHP header location redirect, this is forbidden. Also, for the same reason, when you save the searchpage.php file, save it in ANSI encoding. Do not save it in unicode, unicode big endian, or UTF-8 encoding. These encoding may encrypt and add characters before the PHP code even when there is no white space there. ANSI is the purest encoding that doesn't add any characters before the <?php tag and this will produce the best results. To see a more in-depth article on this PHP header function error, see PHP Header Location Error- Warning: Cannot Modify Header Information- Headers Already Sent.

This is how you create a search engine in PHP.




Related Resources

How to Create Your Own Search Engine For Your Website Using PHP

How to Create a Custom Search Engine For Your Website Using PHP

How to Create an Image Search Engine For Your Website Using PHP

How to Create a video Search Engine For Your Website Using PHP

How to Create an Audio File Search Engine For Your Website Using PHP

How to Upload Images to a Website Using PHP

How to Upload Files to a Website Using PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Confirmation Page for an HTML Web Form Using PHP

How to Insert Images into a MySQL Database Using PHP

How to Create a Searchable Database Using MySQL and PHP

How to Search a MySQL Table For Any Word or Phrase Using PHP

How to Create a Register and Login Page Using PHP

HTML Comment Box is loading comments...