简体   繁体   English

页面之间的数据:$ _SESSION vs. $ _GET?

[英]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. 如果我们想象一个(非ajax)数码相机在线商店,说有人在DSLR部分并指定以图库模式查看相机并按价格订购。 They then click onto the Compact camera's page. 然后他们点击Compact相机的页面。 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) ? 我是否应该注意一些小会话变量的性能问题(即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 使用GET,如果URL被复制粘贴(在电子邮件或MSN中) ,另一个将接收URL的人将具有相同的GET参数
    • is that a good thing, or not ? 这是件好事还是没有?
  • On the other hand, session will not be shared, if an URL is copy-pasted 另一方面,如果URL被复制粘贴,则不会共享会话
    • 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 GET特定于每个URL
    • While SESSION is shared accross all tabs of the user 虽然SESSION在用户的所有选项卡中共享
    • Which means browsing with several tabs at the same time can cause troubles, when using Session, if you don't take care of that 这意味着,如果您不使用Session,那么同时使用多个选项卡进行浏览会导致麻烦

I'd say use both. 我会说两者同时使用。 Store it in the session, but also put it in the get parameters for the page. 将其存储在会话中,但也将其放在页面的get参数中。

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. 通过这种方式,用户可以在页面之间携带他的选项,但是他们也在URL中,因此如果他将搜索结果发送给他的朋友,他的朋友就会以与他完全相同的方式看待他们。

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. 你应该在你的情况下使用GET。

There is one simple rule in the web development: each page with different content must have it's own address. Web开发中有一个简单的规则:每个包含不同内容的页面都必须拥有自己的地址。 So, customer can save any page into favorites, send it to a frend. 因此,客户可以将任何页面保存到收藏夹中,然后将其发送到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. 您可以将用户的偏好保存到他的个人资料/ cookie(不是会话)中,但也应该反映在地址栏中。

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. 我个人会选择会话,因为它不会干扰URL,所以人们可以根据需要为网址添加书签。

However the argument for that would be if they bookmarked it they might see different things if it was done using $_SESSION. 然而,对于那个问题的论点是,如果它们为它添加了书签,如果使用$ _SESSION完成,它们可能会看到不同的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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