-1

I am trying to add 'recent viewed products' feature in my site. So i want to add the products recently watched in cookie. Setting cookie when user go to detail page of the product. So cookie setting code is in detail page. What i am trying

setcookie('recentviews', $productid, time()+3600,'/')

What i need is

recentviews1 =>1
recentviews2 =>5
recentviews3 =>3

OR

recentviews (
[0] => 1,
[1] => 5,
[2] => 3
)

I tried alot of solutions, like setting cookie in loop, but how to know how many views are already set, so I can increment 'recentviews' and set new cookie.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
M Shahzad Khan
  • 935
  • 1
  • 9
  • 22
  • Maybe you can json_encode the array before saving? – Nil'z Sep 19 '13 at 11:01
  • i tried serializing. but how to find how many vies i have already? – M Shahzad Khan Sep 19 '13 at 11:04
  • If you want to persist data just for server-side use sessions. Otherwise serialize, json would be the best suitable format, imho. – Leri Sep 19 '13 at 11:04
  • Yes either `json_encode()` or `serialize()` – Mina Sep 19 '13 at 11:05
  • no, not on server side. On client side. How to do with json? – M Shahzad Khan Sep 19 '13 at 11:06
  • @Kyra, know serialize can help here. but i don't want to loose previous viewed products, so how i can do it in one cookie variable? – M Shahzad Khan Sep 19 '13 at 11:07
  • Is this feature for logged in clients? If so, you may want to store this in a database, as this sort `of data should be persistent regardless of whether the user switches browsers/computers. – Mark Sep 19 '13 at 11:08
  • In response to @Kyslik's response, if you absolutely need to store more than 4kb of data locally, you could look into localStorage (though this would be a JavaScript solution, which isn't tagged, but it's worth mentioning). – Mark Sep 19 '13 at 11:10
  • @hjpotter92, I don't want to loose the products already viewed – M Shahzad Khan Sep 19 '13 at 11:15

2 Answers2

1

I would probably go about something like this (imagining that the cookie content contains obly safe data):

# basic cookie structure:
$recent_views = array(
    # 'product_id' => 'value',
    32324 => 2,
    32455 => 23,
);


# to add a value in a request handler:
$recent_views = json_decode($_COOKIE['recentviews']);
$recent_views[ 32342 ] = 32;
setcookie('recentviews', json_encode( $recent_views ), $expi_time );

Error checking left out for brevity.

Json really has the advantage that you can easily decode the cookie in javascript in the browser and even make changes there.. (not what I would do, but still a possibility).

Cheers -

smassey
  • 5,875
  • 24
  • 37
0

you can follow below example

$info[7][5]=1;
$info[8][5]=1;

Serialize data:

setcookie('cookie', serialize($info), time()+3600);

Then unserialize data:

$data = unserialize($_COOKIE['cookie']);

After data, $info and $data will have the same content.

Janak Prajapati
  • 896
  • 1
  • 9
  • 36
  • 2
    You copied it from here. http://stackoverflow.com/questions/9032007/arrays-in-cookies-php Actually i want to keep track of old views as well. – M Shahzad Khan Sep 19 '13 at 11:17