Domanda

I want to implement a "Remember me" facility for a shopping cart I'm writing. Currently I save state very simply like so:

$user;
if (isset($_SESSION['user']){
    $user = $_SESSION['user'];
} else {
    $_SESSION['user'] = $user = new User();
} 

After the above code, data such as items in the basket can be retrieved via the database like so:

$basket = Basket::obtain($user);

I'm looking for a code snippet or similar that handles the logic of the above, combined with the ability to remember the user over much longer periods (1 year), using cookies that (for example) store a hash, which the server then queries the DB to obtain the $user object state.

I don't trust myself to write this functionality well, so am wondering if there are any light-weight (small footprint) solutions to this problem that will free me from re-inventing the wheel? I avoid large frameworks because they really are overkill and in my experience the learning curve and so on for me is prohibitive.

È stato utile?

Soluzione

You could consider just making a session class that has a few static methods to get an put data into the db? Also agree with Andresch Serj, the learning curve is worth the result.

A framework like codeigniter is pretty easy to learn, documentation is great too.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top