View Full Version : ebay's track what you've view

02-15-2012, 11:53 PM
I often use shopping carts as a memo list generator. I add to cart the products, view cart and print. I've noticed lately many shopping carts don't require an account to do this. You can just add and view right away.

I only need to track the item not anything else. Like ebay's track what you've view with out logging in. Is this possible with js and cookies instead of a db.


02-16-2012, 01:24 AM
You could do this using cookies in Javascript, but that wouldn't be entirely reliable and would be a somewhat complicated script.

The best way to do this would be to use a PHP session (or something equivalent in another serverside language). A session is basically a single "visit" to the website, and during that time if you're using "sessions" in PHP you can store variables. So as you visit pages, just add that page to the list of visited pages and it'll be available on the next page.

Sessions are sort of like simple databases, but they aren't permanently stored anywhere-- just temporarily on the server while that user keeps loading pages. After a period of inactivity the data is cleared from the server.

So just like cookies, sessions don't store the data forever (and shouldn't be relied on for permanent information) but do a great job of having an easy place to keep "notes" about what a user is doing during a visit to the site.

The best thing about sessions in PHP is that they're very easy to use, definitely easier than cookies. Of course you need to know a little about PHP to be able to use them, but if you can deal with that, it's your easiest option.

02-16-2012, 05:16 AM
Could you not "stage" the data on the client with localstorage, then if they want to make the order they "submit" it to the server with the php?

02-16-2012, 06:03 PM

Very Good, now I know where to start my search. It's hard to find something when you don't know where it is. I will look into "sessions in PHP"

djr33 THANKS for your time and knowledge.

02-16-2012, 06:55 PM
Since you're going to use sessions, here's a very quick tutorial. Luckily, sessions are one of the easiest things you could want to learn:

1. For some complicated reasons, sessions need to be activated before there is any output (even a single new line) sent to the browser. Also, they should be used on all pages (even if you're not actively using them) for consistency and to keep the session active during the visit to your site. So, just add the following code to the very top of all of your pages:

<?php session_start(); ?>

2. Once sessions are active, you can use the array $_SESSION to store any information you'd like, any time, on any page, and it will remain active throughout your entire site during the single session for that user. Here's some example code:

$_SESSION['myvariable'] = 'Hello!';

//a while later, or on another page:
echo $_SESSION['myvariable']; //print out "Hello!"

Note: sessions are secure, or at least the data is. No one else can access the data stored in the session, and even the user cannot (as they could with a cookie) unless you choose to output it for them.

Of course you'll need to learn a little more about PHP to deal with things like creating a list of pages they've visited and displaying it in a nice format, but that's really all you need to know about sessions (at least for now).
One very basic point: PHP is basically the same thing as HTML, but it does require that your server has PHP installed and enabled-- most do. Then you need to use the .php extension instead of .html so that the server knows to go through the PHP code. But aside from that, .php is functionally the same as HTML-- what the browser sees is the same as an HTML page (it IS an HTML page, actually). So, for example, you could just rename any HTML page on your site .php and it would be fine. (There's no need unless you want to use PHP on it, though.)