在请求之间干净地传递值(在会话中,作为 GET/POST 参数......?)
我不喜欢在会话中显式管理值,我无法想象我独自一人......所以我想获得以下反馈:
- 其他程序员/平台/框架如何处理这个
- 我使用的方法(解释)下面)
我使用的方法涉及一个控制器类型脚本,当我知道下一个请求中可能需要它时,该脚本支持在会话中添加变量......并在之后自动删除它(TTL通过计数器控制)。
例如,
- 请求 1 - 脚本使用键
selectedValue
添加一个值到会话中 - 请求 2 - 脚本从会话中读取
selectedValue
- 请求 3 -
selectedValue
已从会话中消失(这没关系,因为不再需要)
这是我能想到的通过不同请求传递值的最干净的方式,而不是存储全局变量会话中(例如,经过身份验证的用户 ID)。
在这种情况下,页面刷新将被忽略,如果需要将值传递给进一步的请求,则需要再次设置。
I don't like having to explicitly manage values in session and I can't imagine I'm alone in this... so I wanted to get feedback on:
- How other programmers/platforms/frameworks handle this
- The method I use (explained below)
The method I use involves a controller-type script that supports adding a variable in session when I know it may be needed in the next request... and automatically removes it afterwards (the TTL is controlled through a counter).
For example,
- Request 1 - script adds a value to the session with key
selectedValue
- Request 2 - script reads
selectedValue
from the session - Request 3 -
selectedValue
is gone from the session (this is OK because it's no longer needed)
This is the cleanest way I can think of passing values through different requests, as opposed to storing global variables in session (e.g., an authenticated user ID).
In this scenario, a page refresh is ignored, and if a value needs to be passed through to further requests, it needs to be set again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也可以将其作为请求参数传递给后续请求,而不是将其存储在会话中。假设您为此使用表单,则
对此很有用。
例如,
您可以通过
$_POST['foo']
保留它。Instead of storing it in the session, you could also just pass it as a request parameter to the subsequent request. Assuming that you're using forms for this, the
<input type="hidden">
useful for this.E.g.
You can retain it by
$_POST['foo']
then.在调试 $_SESSION 相关问题的糟糕经历之后,我选择了隐藏字段路线,但是现在我更有经验了,我认为会话变量是更好的方法。我宁愿显式管理 $_SESSION 中的值,也不愿对隐藏字段执行基本相同的操作,如果您想在多次刷新时保留该信息,则必须在每次页面加载时重新编码该信息。
I went the hidden field route after having bad experiences debugging $_SESSION related issues, however now that I'm more experienced I think session variables are the better way to go. I'd rather explicitly manage values in $_SESSION than do essentially the same thing with hidden fields where you have to re-encode that information at every page load if you want to retain it over multiple refreshes.