简体   繁体   English

在PHP页面之间传递类实例和其他数据

[英]Passing class Instances and other data between pages in PHP

I've been looking into the problems of having persistent data available between pages in PHP. 我一直在研究在PHP页面之间具有持久数据可用的问题。 This particularly applies to objects that have been set up in one page that need to be accessed later. 这尤其适用于已在一页中设置的对象,以后需要访问。 It seems this is more difficult than I assumed it would be, but there are several ways this could be done, although they all seem a bit awkward to use especially when the data gets quite complex: 看来这比我想象的要困难得多,但是可以用多种方法来完成,尽管它们似乎都很难使用,尤其是当数据变得非常复杂时:

  • Passing the data via $_GET or $_POST to the next page 通过$ _GET或$ _POST将数据传递到下一页
  • Copying the data to a database and retrieving it in the next page 将数据复制到数据库并在下一页中检索它
  • Putting the data in a session or cookie 将数据放入会话或cookie
  • Serializing the object and recreating it with the same parameters and values 序列化对象并使用相同的参数和值重新创建它

These all seem quite laborious as they mostly rely on having to deconstruct your existing data structure and then rebuild it again on the next page. 这些似乎都很费力,因为它们主要依赖于必须解构现有数据结构,然后在下一页重新构建它。 I assume this is to reduce memory requirements of the PHP server by purging data from one page as soon as its closed and starting with a 'clean slate'. 我认为这是为了通过关闭页面后立即从一页清除数据并以“干净的状态”开始减少PHP服务器的内存需求。

Is there a more direct way of passing larger data structures between pages in PHP? 有没有更直接的方法可以在PHP页面之间传递较大的数据结构?

Many thanks, Kw 非常感谢,Kw

I assume this is to reduce memory requirements of the PHP server by purging data from one page as soon as its closed 我认为这是为了通过关闭后从一页清除数据来减少PHP服务器的内存需求。

Nope, this is not because of memory efficiency concern. 不,这不是因为内存效率问题。 This is because HTTP protocol is stateless. 这是因为HTTP协议是无状态的。 Each request must carry all information that is necessary to fulfill it. 每个请求必须包含实现它所必需的所有信息。

Counter-example to your proposed scenario: 您提出的方案的反例:

  1. let's suppose Alice visits page A, some objects are created and you want them to be available in page B. 让我们假设爱丽丝访问页面A,创建了一些对象,并且希望它们在页面B中可用。

  2. You track a visit to page B. 您跟踪对页面B的访问。

    2.1. 2.1。 But it's not Alice, it's Bob. 但这不是爱丽丝,是鲍勃。 How do you determine which objects to show and where do you get them from? 您如何确定要显示的对象以及从何处获取对象?

    2.2. 2.2。 It is Alice again, but the request arrived to another machine from your 1000 server farm. 再次 Alice,但是请求从您的1000服务器场到达了另一台计算机。 Naturally, you don't have original PHP objects. 自然,您没有原始的PHP对象。 What do you do now? 你现在做什么?

If you use $_GET or $_POST you are limited to non-sensitive data and you expose your objects to any user. 如果使用$ _GET或$ _POST,则仅限于非敏感数据,并且会将对象公开给任何用户。 You don't want that. 你不要那样

Cookies are limited in size Cookie的大小受到限制

cookies are usually limited to 4096 bytes and you can't store more than 20 cookies per site. Cookie通常限制为4096个字节,并且每个站点最多只能存储20个Cookie。

The best way to persist objects between requests (for the same user) is to use Sessions. 在请求之间(对于同一用户)在对象之间持久保存对象的最佳方法是使用会话。 There are already session save handlers for memcached, redis, mysql etc. You can also write your own if you need something custom. 已经有用于memcached,redis,mysql等的会话保存处理程序。如果需要自定义,也可以编写自己的会话保存处理程序。

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

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