views:

40

answers:

4

I'm not sure if there is an explanation on SO for beginners, but if so, could you provide the links.

I am interested in creating a "recent viewed" function that shows what links they clicked on before. I'm not sure if this is the 'correct' way to do it, but this is what I have so far...

  1. user clicks on a link(say ad.php?posting_id=12).
  2. when the user clicks on the link, it sets a cookie for $_cookie['ad.php?posting_id=22']
  3. Each time the user clicks, more cookies are set
  4. In the recent viewed function, it gets all the $_cookie variables and displays them.
  5. if the user wants to clear the history, just destroy all the cookies

I'm not sure if this is the way to do it, but is the viable? If not, what are the steps to create a "recent viewed" function

A: 

Just store the request history in a database or something, then basically do:

SELECT requestURI from requestlog WHERE userid=<theuser> ORDER BY date DESC LIMIT 0,10

That is how I would implement something like that.

webdestroya
will do! 4 answers in like 5 minutes...love SO :)
ggfan
I'm not sure that's a good idea. You'll end up needing a strategy for deleting out this transient data and the more users / pages you serve the bigger this table is going to get. The cookie idea is much better.
Sohnee
A: 

This will work fine but a user can clear their cookies by themselves. If you don't want this to happen you should store the sites in a database

Galen
Alright, thanks! sounds a lot better. I guess if the user is a guest, I would store it in cookies.
ggfan
A: 

It's all fine except for step 2, which is no step at all. Just remove that (and change "3. if the user" to "3. When the user")

egrunin
+2  A: 

You're probably going to want to just store the IDs of those articles in the cookie. No point in storing the path. I imagine you're going to want more information than just the path (title, for instance) which is going to require a db call.

dclowd9901
Yes, I am just storing "posting_id=12", was just having the whole URL here for clarity.
ggfan