简体   繁体   中英

data between pages: $_SESSION vs. $_GET?

Ok, firstly this is not about forms this is about consistent layout as a user explores a site.

let me explain:

If we imagine a (non-ajax) digital camera online store, say someone was on the DSLR section and specified to view the cameras in Gallery mode and order by price. They then click onto the Compact camera's page. It would be in the users interests if the 'views' they selected we're carried over to this new page.

Now, i'd say use a session - am i wrong?

are there performance issues i should be aware of for a few small session vars ( ie view=1 , orderby=price) ?

Speaking of performances, there should not be much problems with either solutions.

Some things that have to be considered are :

  • With GET, if an URL gets copy-pasted (in a email or MSN) , the other who will receive the URL will have the same GET parameters
    • is that a good thing, or not ?
  • On the other hand, session will not be shared, if an URL is copy-pasted
    • which means the first guy will say to the other " key, look at this ", and the second guy will not see the same page ;; same thing with bookmarking, should I add.
  • GET is specific to each URL
    • While SESSION is shared accross all tabs of the user
    • Which means browsing with several tabs at the same time can cause troubles, when using Session, if you don't take care of that

I'd say use both. Store it in the session, but also put it in the get parameters for the page.

Why? This way the user is able to carry his options from page to page, but they are also in the URL so if he sends search results to his friend, his friend sees them the exact same way he did.

No, the session's performance will not degrade by putting those small variables in there. Unless you're storing monolithic arrays in your session, the vast majority of the time loading a session will be reading it from its storage medium (file, database, memcache, etc).

You should use GET in your case.

There is one simple rule in the web development: each page with different content must have it's own address. So, customer can save any page into favorites, send it to a frend. It's pain in the bottom then someone sends you a link to a particular page saying "Look!" but site uses frames and you land at the front page and dunno where to look.

You can save user's preferences into his profile/cookie (not session), but it should be reflected in the address bar as well.

Sessions being used for completely different purpose, shopping cart is an example.

It's a subjective question, it would work either way.

Personally I would go with sessions as it doesn't interfere with the URL so people can bookmark the url if they wanted.

However the argument for that would be if they bookmarked it they might see different things if it was done using $_SESSION.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM