• 0

Sessions tutorial wanted


Question

I've read a couple of tutorials and I couldn't find one that seemed to work with an undefined number of variables.

I'm coding a shopping cart, so there can be 1, 2, 20 items in the shopping cart. I currently add a line in a separate table (containing the session id, the item id, and the quantity) so I can look up in my table with MySQL for all the lines from the session id. However with sessions, I didn't find anything that would let me search for all the entries in the cookie.

Thanks in advance

kag

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Have a Carts SQL table.

On the cart pageload, check the SID (session ID) and return all rows from the Carts table that apply to that SID.

Think: mysql_query('SELECT * FROM Carts WHERE `session_id` = \''.session_id().'\'')

When someone adds an item to their cart, add it to the Carts table, with the SID and Item # and Quantity.

I suppose you could also do it with $_SESSION variables. Such as $_SESSION['item1'], $_SESSION['item2'] etc.

Loop through it like in this code:

for ($i = 1;;$i++) {

if ($_SESSION['item'.$i]) {

print('Item '.$i.' exists!');

}

else {

break;

}

}

I don't think you can turn $_SESSION into a multidimensional array...but i think the code above would work if you wish to use $_SESSION vars without any sql stuff

good luck.

Link to comment
Share on other sites

  • 0
Have a Carts SQL table.

On the cart pageload, check the SID (session ID) and return all rows from the Carts table that apply to that SID.

Think: mysql_query('SELECT * FROM Carts WHERE `session_id` = \''.session_id().'\'')

When someone adds an item to their cart, add it to the Carts table, with the SID and Item # and Quantity.

I suppose you could also do it with $_SESSION variables. Such as $_SESSION['item1'], $_SESSION['item2'] etc.

Loop through it like in this code:

for ($i = 1;;$i++) {

if ($_SESSION['item'.$i]) {

print('Item '.$i.' exists!');

}

else {

break;

}

}

I don't think you can turn $_SESSION into a multidimensional array...but i think the code above would work if you wish to use $_SESSION vars without any sql stuff

good luck.

Well maybe I didn't explain it correctly, but that's exactly how I do it right now. I want to change over to sessions for 2 reasons

1 - I believe that sessions are faster than doing mysql queries. If the site becomes busy, it won't throw the server on its knees.

2 - When I use a cart table like you said (and like i currently do), you have to empty the table every now and then. When a user orders the stuff from their shopping cart, the entries are deleted from the table, but when they leave before without ordering, everything stays there and on the long run, it's gonna make the table very big if you dont clean it up.

Looking at your $_SESSION example, it seems like a good idea.

Edited by kag
Link to comment
Share on other sites

  • 0

i was going to put this in but i didn't want to make my post too long/confusing. and i also had a feeling you didn't want to use SQL.

BUT...

if you really wanted to you could have an additional table called Sessions where you keep a list of all the sessions and the last time() they were accessed; on each pageload you'd have to find the sessions that are more than x seconds old and then delete it from both the sessions table and the cart table.

or if you're feeling especially daring, have a time column on the cart table and have no sessions table. think:

delete from cart where time = time() - 3600

3600 can be any value of course, that's an hour.

and on each pageload you'd have to update the rows like:

UPDATE carts SET time = time() WHERE sid = session_id()

Link to comment
Share on other sites

  • 0

Mmm, ingenius.

I know it will work both ways, but what do you think is the "proper" way to do it? With sessions or with a session/time thingy in the table? Or everyone have their own way?

Link to comment
Share on other sites

  • 0

there's a good question.

i don't have enough experience with heavy-load situations as of yet. i've only started doing php/mysql about a month or two ago.

personally, i think i would take the mySQL route (with one table). i think session vars are intended to store smaller amounts of data (on a global scale, that is, for all of the users)

also i'm not sure how PHP destroys session data. if you firmly said for mysql to delete anything that hasn't had anything happen in an hour, then you KNOW it's gone and not taking up space, and i'm not sure you can do that with PHP (check the PHP manual)

i know you can delete the current session's data. but yeah, not sure.

i'm hardly ever a fan of the whole item1,item2 thing. it just seems messy/prone to fault when you do 'item'.$i

it just seems that the SQL route is much more mainstream. but then again, this would be opening a socket on each pageload (well, cartpage load). unless you use persistent connections, which i'm not sure of that either. this falls into something that i will be needing to know in the fairly near future... does it make sense to have like 15 persistent connections and choose a random one on each pageload for really heavy-load situations? i assume PHP can process >1 page at a time, and one persistent connection might lag things up more, but what about multiple connections? that was just an idea i had for a situation i'll be needing to handle in the near future.

if anyone else has any thoughts on this please comment...

Link to comment
Share on other sites

  • 0
i'm hardly ever a fan of the whole item1,item2 thing. it just seems messy/prone to fault when you do 'item'.$i

I don't understand what you mean. I've never used that syntax, but from what I can see, 'item'.$i should be used when you don't know how many variables you have (meaning that if you use item1, item2, etc., you don't know when to stop).

For the destroy thing, you can use session_destroy() to destroy the current session and session_unset() to unset its global variables.

As for the rest of your post, I'm asking myself the same questions.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.