Show recent viewed product list on website in php

I am writing down the code to show the recent viewed product on the e-commerce websites.

if(!$_COOKIE[‘recentviews’]){
setcookie(‘recentviews’,$_GET[‘pid’],time()+86400);
}else{
$cArray=explode(“,”,$_COOKIE[‘recentviews’]);
if(!in_array($_GET[‘pid’],$cArray)){
setcookie(‘recentviews’,$_COOKIE[‘recentviews’].”,”.$_GET[‘pid’], time()+86400);
}
} ?>

Note:before using $_COOKIE start the session ie write down the session_start() on the top of the page.

$query=”select * from product where id in(“.$_COOKIE[‘recentviews’].”) AND id!=’”.$_GET[‘id’].”‘”;

Explanation:

Suppose i am on the page with url like http://www.example.com?productdetail.php&id=32
where 32 is the id of the product i am viewing.

We are using cookies to track the user browsing history(Note: this code will not work if user has disabaled cookie in his browser).

we are storing the id of the product which user is viewing. we are creating a string like “234,43,788,…..” this will store the all ids of the product.

The below query will show all the product which are viewed by the user excluding the current product.
$query=”select * from product where id in(“.$_COOKIE[‘recentviews’].”) AND id!=’”.$_GET[‘id’].”‘”;

READ  Fancybox on ASP.net Web Forms

Leave a Reply

Your email address will not be published. Required fields are marked *