How to Redirect to Another URL Using PHP



PHP


In this article, we show how to redirect a user to another URL (page) using PHP.

The way to accomplish this to use the location header () function. Using this function, a URL redirect can be done in PHP. So we fully demonstrate how to do this.

We'll illustrate this using a search text box where a user types in a term and presses the submit button and then is redirected by the search box to the URL he is looking for. For example, a user may type in the phrase, 'google', and then be redirected to the google homepage.

Executing a redirect in PHP isn't difficult. Only that is needed is a simple header location function. If statements are used to redirect a user to a certain page, as specified by the search query.

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

Search Engine

Enter Product Name


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, "amazon", we then need to redirect him to the amazon 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 list of if statements. If it does, we then direct the user to the necessary URL 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 results found for this search".

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 results found for this search 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.

So these redirects work well. The search box redirects a user to a certain page if the user types in the exact phrase desired. If you want to add more flexibility, instead of using the direct if statement, for example, if ($result == "amazon"), you can use if (strpos($result, "amazon") == "true"), this way the search query doesn't need to be a direct match in order to redirect to the amazon page. Instead if the user types in a phrase that contains the word 'amazon' such as 'amazon website' or 'amazon homepage', s/he will be redirected to the amazon site. If the direct (==) if statement, the user is only directed to the amazon website if the search query is directly 'amazon'. For more details on this, see How to Create a Search Engine Using PHP. This shows in-detail how to create an advanced search box that can return increasingly relevant results.

Now we have a search box that does redirects 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'.

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 URL redirects can be achieved easily with PHP.




Related Resources

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

HTML Comment Box is loading comments...