在请求之间干净地传递值(在会话中,作为 GET/POST 参数......?)

发布于 2024-10-24 15:51:30 字数 539 浏览 7 评论 0原文

我不喜欢在会话中显式管理值,我无法想象我独自一人......所以我想获得以下反馈:

  1. 其他程序员/平台/框架如何处理这个
  2. 我使用的方法(解释)下面)

我使用的方法涉及一个控制器类型脚本,当我知道下一个请求中可能需要它时,该脚本支持在会话中添加变量......并在之后自动删除它(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:

  1. How other programmers/platforms/frameworks handle this
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2024-10-31 15:51:30

您也可以将其作为请求参数传递给后续请求,而不是将其存储在会话中。假设您为此使用表单,则 对此很有用。

例如,

<form action="confirm.php" method="post">
    <input type="submit" name="confirm" value="Confirm" />
    <input type="hidden" name="foo" value="<?=htmlspecialchars($foo)?>" />
</form>

您可以通过 $_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.

<form action="confirm.php" method="post">
    <input type="submit" name="confirm" value="Confirm" />
    <input type="hidden" name="foo" value="<?=htmlspecialchars($foo)?>" />
</form>

You can retain it by $_POST['foo'] then.

唱一曲作罢 2024-10-31 15:51:30

在调试 $_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文