New guy :D: $_Session and numerical data.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TeamMCS
Forum Newbie
Posts: 5
Joined: Wed Jan 05, 2005 8:09 am

New guy :D: $_Session and numerical data.

Post by TeamMCS »

Hey guys, if you've ever visited phphelp.com you may know me. PHP forums have gone down for some bizzar reason so I was suggested to come here :D.


Anyway, I'm developing a shopping basket problem is I cant seem to enter the product id into an multidimentional session.. for instance...

$_SESSION['basket']['item_2'] = 3;

will return 1, infact what ever number I put in seems to return as 1.. I've checked the code and it accepts pure chatares so I could write "bleh" and it would return "bleh", but if I wrote "bleh3" it would return "1bleh"...


Does anyone know why this is?

-Thanks Miles
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that sounds very strange, can you post your code?
TeamMCS
Forum Newbie
Posts: 5
Joined: Wed Jan 05, 2005 8:09 am

Post by TeamMCS »

I think I figured it out, it has something to do with mutiple dimention sesions...


I had a 4 dimentioned session... when I made that a fith dimention it seemed to limit the charaters it could handle..

I suspect PHP has allocated the whole session string a certain size, once you use up that space it cuts off the remaining code.

Now I've dropped the dimentions to 3, 4 at most. It works fine, but I suspect if I tried to add much more data to the highest dimnetion it would cull it.

Just something to keep in mind when using sesions
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

true, sessions do have an upper limit to their size. I'd suggest storing that data in a database, or figuring out how to minimize the space requirements. In order to see the actual size of your session data, use [php_man]serialize()[/php_man] on it like so:

Code: Select all

$pack = serialize($_SESSION);
I know for a fact, multidimensional arrays use a lot of extra bytes when stored.

You may want to consider creating your own session data handler, so you can compress the data or use other packing methods to minimize the size. Alternately, you can use a database to store the information.
TeamMCS
Forum Newbie
Posts: 5
Joined: Wed Jan 05, 2005 8:09 am

Post by TeamMCS »

This may be a big problem for me, the site i'm designing is shopping suite. A LOT of stuff is getting stored on those sessions.

I wonder how much data the session can store before it dies.

http://home.mnetcs.com/dev/hardware/index.php

^^ for anyone interesting, it is a test site so not a lot is working :P
keiths
Forum Newbie
Posts: 7
Joined: Fri Dec 24, 2004 8:46 pm

Post by keiths »

I think this was mentioned earlier but you might want to create a cart table in the database and store everything that is being ordered in it.

When doing something with ordering, I like to store the cart in the database. That way if a customer is ordering something and fills out their person information but drops out before the credit card or confirmation page, you can attach the cart to that customer. Then if they don't purchase the product in a week or so, you can send them an email asking them if there was a problem.

There's a lot of other good reasons for storing the cart in the database also.
TeamMCS
Forum Newbie
Posts: 5
Joined: Wed Jan 05, 2005 8:09 am

Post by TeamMCS »

Yeah I had thought about using a database for the cart system, but there are many problems agianst using it. Such as the build up of useless data.

Once the user confirms they want that stuff it can be converted to a quote which is saved in the database.

I guess it'll be one of those things I'll have to wait and see. The modularization of ths suite should allow me to make tha changed should I need to...

Thanks for the info though, gave me a few ideas there 8)
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

dont forget it would be easy to set up a database garbage cleanup script and run it via cron periodically
TeamMCS
Forum Newbie
Posts: 5
Joined: Wed Jan 05, 2005 8:09 am

Post by TeamMCS »

Its not so much that, but sessions are so insainly easy and easily solve any stoage, connection or security issues which can be an issue with databases.

To get the cart working on simple sessions only requires at most 5 lines of code...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

getting a cart working with a database doesn't take that much more than 5 lines, depending on how you implement the hooks.
Post Reply