How to show recent searches done by a user on your website. I am using cookies to show recent searches performed by a user. In this, when you show recent searches I have limited it to 5 in descending order i.e. latest 5 are shown. One can also show Search Type like you are searching a product, category or something else. So, here it is being shown in braces (). Actual search is shown with a link to the search.
There are two pages, to set and get cookie and another one to show searches.
As, we know that for cookies we need to set it before any output on the page include first page i.e. cookie_search.php before any output and cookie_search_display.php anywhere you want to display results.
cookie_search.php
[php]
<?php
if(isset($_COOKIE[‘cookie_search’]))
{
$cookie_search_count=count($_COOKIE[‘cookie_search’]);
}
else
{
$cookie_search_count="0";
}
$cookie_search_set_sts=1;
if(isset($_REQUEST[‘searchsub’]))
{
if(isset($_REQUEST[‘searchterm’]))
{
if($_REQUEST[‘searchterm’]!=”)
{
$cookies_searchterm=$_REQUEST[‘searchterm’];
}
else
{
$cookie_search_set_sts=0;
}
if(isset($_REQUEST[‘searchtype’]))
{
$cookies_searchtype=$_REQUEST[‘searchtype’];
if($cookies_searchtype==”)
{
$cookie_search_set_sts=0;
}
}
else
{
$cookies_searchtype="";
$cookie_search_set_sts=0;
}
}
else
{
$cookie_search_set_sts=0;
}
}
else
{
$cookie_search_set_sts=0;
}
if($cookie_search_set_sts==1)
{
$cookie_search_expire=time()+60*60*24;
setcookie("cookie_search[$cookie_search_count][0]", $cookies_searchterm, $cookie_search_expire);
setcookie("cookie_search[$cookie_search_count][1]", $cookies_searchtype, $cookie_search_expire);
}
// after the page reloads, print them out
if(isset($_COOKIE[‘cookie_search’]))
{
$cookie_search_display=array();
//echo "<pre>";
//print_r($_COOKIE[‘cookie_search’]);
//$cookie_search_display=array_unique($_COOKIE[‘cookie_search’]);
$cookie_search_display=$_COOKIE[‘cookie_search’];
//echo "<pre>";
//print_r($cookie_search_display);
$cookie_search_count=count($cookie_search_display);
}
?>
[/php]
Explaination:
- cookie_search – Cookie Name
- searchsub – a hidden field value in search submit to see if a search has been submitted. This should be in search forms for which you want to track
- searchterm – Search input field name
- searchtype – a hidden field value in search submit with value as search type to show in braces (i.e. product, category etc.)
cookie_search_display.php
[php]
<h1>Your Recent Searches</h1>
<?php
if($cookie_search_count>’0′)
{
$cookie_search_count_from=$cookie_search_count-1;
$cookie_search_count_to=$cookie_search_count-6;
if($cookie_search_count_to<‘0′)
{
$cookie_search_count_to="0";
}
echo "<ul>";
for($i=$cookie_search_count_from; $i>=$cookie_search_count_to; $i–)
{
//$name = $cookie_search_display[$i];
$cookie_searchterm_value = $cookie_search_display[$i][0];
$cookie_searchterm_type= $cookie_search_display[$i][1];
if($cookie_searchterm_type==’Products’)
{
$cookies_searchtermurl="search/";
}
elseif($cookie_searchterm_type==’Category’)
{
$cookies_searchtermurl="search/category/";
}
else
{
$cookies_searchtermurl="search/";
}
echo "<li><a href=’$cookies_searchtermurl$cookie_searchterm_value’>$cookie_searchterm_value</a> ($cookie_searchterm_type)</li>";
}
echo "</ul>";
}
?>
[/php]
Explaination:
- $cookie_searchterm_type – Retrieving Search Type value like – Products, Category etc.
- $cookies_searchtermurl – Setting search Type URL
- $cookie_searchterm_value – Search Term