在 HTML/PHP 中传递变量的推荐方法?

发布于 2024-10-05 10:13:33 字数 69 浏览 5 评论 0原文

嗨我只是有一个简单的问题: 在您的网站上工作时传递变量的推荐方法是什么? - 获取、发布、会话、cookie、隐藏字段...

Hi I just have a simple question:
What is the recommended way to pass variables when working on your site.
- get, post, session, cookies, hidden fields, ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

季末如歌 2024-10-12 10:13:33
  • GET 显示数据时
  • POST 修改数据时
  • 会话时 在请求之间存储数据时,会话过期后不需要保留在
  • 数据库(或其他持久存储)中,用于需要在会话之间保留的数据

Cookie;或多或少从来没有。您放置在那里的任何内容几乎总是可以更好地存储在会话中。也许仅适用于持久登录。

隐藏字段;再次强调,几乎从不使用会话并将数据安全地保存在服务器上。有时用作通过 javascript 收集的数据的存储点,但仅此而已。

  • GET when showing data
  • POST when modifying data
  • session when storing data between requests that doesn't need to stay after the session expires
  • database (or other persistent storage) for data that needs to hang around between sessions

Cookies; more or less never. Anything that you would put there could almost always be better stored in the session. Perhaps for persistent logins, only.

Hidden fields; again, almost never, use a session and keep the data safe on the server. Sometimes used as storage points for data collected via javascript, but that's it.

明天过后 2024-10-12 10:13:33

我更喜欢会话。这是您上面提到的服务器端的唯一选项。

如果您想将信息从客户端传输到服务器,您可以使用 POST 或 GET。请记住,隐藏字段最后将成为 get 变量的 post。

Get、Post、Cookies 和隐藏字段都可以相对简单地操作。您选择哪个选项,请确保始终检查变量是否有效。用户输入的值永远不可信!

I prefer Sessions. It is the only option you mention above that is server side.

If you want to transfer information from your client to your server you could use either POST or GET. Remember that a hidden field in the end will become a post of get variable.

Get, Post, Cookies and hidden fields can all be manipulated relatively simple. Which option you choose, make sure you always check your variables to be valid. User input values can never be trusted!

一身软味 2024-10-12 10:13:33

所有这些东西都有不同的用途,有时甚至是重叠的用途。选择一个并在开发完整的应用程序时仅使用它会导致极其糟糕的误用。你需要一个更具体的问题。

All of those things have different, sometimes overlapping uses. Choosing one and using only that in the development of a complete application would lead to ridiculously bad mis-usage. You need a more specific question.

穿越时光隧道 2024-10-12 10:13:33

我通常坚持一些简单的规则:

GET - 用于获取信息。

例如:

  • site.com/articles/category/2site.com/articles.php?category=2 显示第二类别的所有文章

  • site.com/search/mikesite.com/search.php?q=mike 在网站上搜索 mike

- 用于更新或插入数据

隐藏字段由于各种原因在 POST/GET 表单中使用,通常用于 ID 或类似此

会话的内容 - 用于一个会话的数据。存储一些用户偏好、用户数据。

Cookies - 用于“记住我”功能和一些 JS 内容(因为 JS 无法访问会话数据)

PS 还有 PUT 和 DELETE 方法,但一些共享托管商不允许它们......

I usually stick to some simple rules:

GET - for getting information.

For example:

  • site.com/articles/category/2 or site.com/articles.php?category=2 shows me all articles for second category

  • site.com/search/mike or site.com/search.php?q=mike searches for mike on site

POST - for updating or inserting data

Hidden fields used in POST/GET forms for various reasons, often for IDs or something like this

Session - for data for one session. Storing some user preferences, user data.

Cookies - for "remember me" functionality and some JS stuff (because JS can't reach Session data)

P.S. There are also PUT and DELETE methods, but some shared hosters don't allow them...

耳根太软 2024-10-12 10:13:33

取决于站点以及您何时需要传递数据。

如果它在页面上加载 cookie,如果您有会话,如果您提交表单(HTTP POST 或 GET),如果提交并且您不希望用户看到,则使用表单的隐藏字段(HTTP POST 或 GET),通过 URL (GET),最后,如果它是 AJAX,例如 Web 应用程序,您可以使用多个选项和方法(HTTP GET、PUT、POST 和 DELETE - 请参阅 RESTful APIS 以获得一个很好的示例)。

请注意,对于所有 HTTP GET、PUT、POST 和 DELETE 方法,您仍然可以使用 cookie 和会话

Depends on the site and when you need to pass the data.

If its on page load a cookie, if you have sessions a session, if your submitting a form (which is HTTP POST or GET), if submitting and you dont want the user to see use a hidden field with a form (which is HTTP POST or GET), via the URL (GET) and lastly if its a AJAX eg web app you have several options and methods open to use (HTTP GET, PUT, POST and DELETE - see RESTful APIS for a good example).

Note with all HTTP GET, PUT, POST and DELETE methods you can still use cookies and sessions as well

苍白女子 2024-10-12 10:13:33

会话是传递特定值的最合适的方式。但如果您想一次发送多个变量,那么 post 是最好的方法。

Session is the most appropriate way to pass spacific values. But if you want to send number of variables in a single time then post is the best method.

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