Cookies taste awful

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
chemoautotroph
Forum Newbie
Posts: 3
Joined: Mon May 12, 2003 4:08 pm

Cookies taste awful

Post by chemoautotroph »

For some reason whenever I store more than 3 items in a cookie array, it starts deleting old items. Why and how do I fix it??

This is really bad, because I'm using cookies to create a shopping cart, and after 3 items are added, the old ones disappear- I don't think my customer would appreciate that.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

why are you storing shopping cart info in cookies? tis is bad idea. Look into using a database or at least a session array.

I run an ecommerce site and we have many customers who don't enable cookies(they think we're going to cyber attack them lol).
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Cookies have a fixed maximum filesize of somthing around 4Kb (or around that value). If you're using PHP, don't use cookies to store data, there are plenty of other ways:
sessions
serialization
database (provided you have access to a database)
etc.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

sessions are tricky. cookies store more than get. get is session's second method, post being the first. cookies being the third.

the database or using a text file are probalby the best way
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

m3rajk wrote:sessions are tricky. cookies store more than get. get is session's second method, post being the first. cookies being the third.

the database or using a text file are probalby the best way
I wouldn't call sessions tricky.

In the "standard" php install, cookie is the first session method, not the last. Sessions's fall back to GET is the cookie is rejected. POST propagation is only used when a form is detected and is seperate from the GET/Cookie decision.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

that's why i call it tricky. first off it uses three other methods, secondly the book i have lists them the way i listed them, and then there's people and places like php.net that has a different opinion..


still, since get is limited and so are cookies and i know post is, but it's a third one greater than get and i don't know how it compares to cookies, i'd say use a text file for the person if you don't have a db, but kow that if you expand past a few customers on at a time the size of the text file directory will get very large very fast. a db is MUCH better, and you can give an order number as well as user number so someoen can go back an look at their previous orders, etc
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

m3rajk wrote:that's why i call it tricky. first off it uses three other methods, secondly the book i have lists them the way i listed them, and then there's people and places like php.net that has a different opinion..


still, since get is limited and so are cookies and i know post is, but it's a third one greater than get and i don't know how it compares to cookies, i'd say use a text file for the person if you don't have a db, but kow that if you expand past a few customers on at a time the size of the text file directory will get very large very fast. a db is MUCH better, and you can give an order number as well as user number so someoen can go back an look at their previous orders, etc
Well whatever php.net or zend.com lists should be taken as more authoritative than any third-party book.

The point is that while there is a lot that goes on behind the scenes when using sessions, the developer can "forget" about it most of the time. (Yes the developer should eb aware of it)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Sessions are not tricky.

1. You start the session with "session_start()" at the beginning of the page, you assign a session-variable with $_SESSION["myVariable"]='bla'.
Nothing tricky so far.

2. Now, as Jason states in his sticky, HTTP is a stateless protocol, every page needs to re-register the session with the server. Hence you need session_start() on every page.

3. To identify the session, either a cookie is set on the clients computer containing the session-id, if not, the session-id will be put into the URL via GET.

All a PHP-programmer needs to worry about is step 1 and 2. Step 3 happens automatically.

There is nothing tricky about sessions.
chemoautotroph
Forum Newbie
Posts: 3
Joined: Mon May 12, 2003 4:08 pm

Post by chemoautotroph »

I pretty much figured that $_SESSION was the way to go, I started converting the site over to them before even reading your replies. Thanks anyway.
Post Reply