会话和cookie通常被视为输入吗?

发布于 2024-12-22 11:50:45 字数 243 浏览 0 评论 0原文

我认为 $_GET$_POST 是用 PHP 编写的 Web 应用程序中的输入。这是因为它们包含用户可以定义并且随 HTTP 请求“附带”的值。但是session和cookie是一样的吗?

当我阅读维基百科 PHP 意大利语页面时,我想到了这个问题,该页面说常见输入是 $_GET$_POST$_SESSION

I consider $_GET and $_POST to be input in a web application written in PHP. That's because they contain values that the user can define and that "comes" along with the HTTP request. But are sessions and cookies the same?

This question came to me when I was reading the wikipedia PHP italian page that says that common inputs are $_GET, $_POST and $_SESSION.

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

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

发布评论

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

评论(4

半城柳色半声笛 2024-12-29 11:50:45

如果对于输入,您的意思是“请求中出现的所有内容”(因此,需要验证的内容等),则应包括:

  • POST/GET vars ($_POST, $_GET)
  • Cookie ($_COOKIE)
  • 请求路径 ($_SERVER["REQUEST_URI"])
  • HTTP 方法($_SERVER["REQUEST_METHOD"])
  • HTTP 标头(通常在 $_SERVER["HTTP_"*] 中)

会话是一种特殊情况,因为所有会话变量都是存储在服务器端,即用户无法像使用 cookie 那样修改它们。无论如何,客户端会保存一个 cookie 来存储会话 ID,用户可以将其重置/设置为自定义值。

更新 - 关于 $_REQUEST

如前所述,在 PHP 中您还可以访问 $_REQUEST,它是来自 $_GET 的变量的组合, $_POST$_COOKIE
$_REQUEST 的具体内容由 php.ini 中的 request_order 指令确定。

If for input you mean "all the things coming in the request" (so, things that need validation, etc.), you should include:

  • POST/GET vars ($_POST, $_GET)
  • Cookies ($_COOKIE)
  • The request path ($_SERVER["REQUEST_URI"])
  • The HTTP method ($_SERVER["REQUEST_METHOD"])
  • The HTTP headers (usually in $_SERVER["HTTP_"*])

The session is a special case since all the session variables are stored on the server-side, i.e. the user cannot modify them as it would with cookies. Anyways, a single cookie is saved on the client-side to store the session ID, that can be reset / set to a custom value by the user.

Update - about $_REQUEST

As pointed out, in PHP you also have access to $_REQUEST, that is a mix of variables from $_GET, $_POST, $_COOKIE.
The exact content of $_REQUEST is determined by the request_order directive in php.ini.

梦在夏天 2024-12-29 11:50:45

$_SESSION 仅包含您自己放在那里的内容。但是,$_COOKIE 应与 $_GET、$_POST 一起被视为不受信任的用户输入。

我能想到的交叉点是最终用户尝试通过 $_COOKIE 提供精心设计的 PHPSESSID,以期获得对其他用户会话的访问权限。

除非您直接将其他输入($_GET, $_POST, $_COOKIE)存储到$_SESSION中,否则它是可信的。

// Oops, $_SESSION['someval'] now holds unfiltered user input!
$_SESSION['someval'] = $_POST['someval'];

$_SESSION only contains what you yourself put there. $_COOKIE, however, should be considered untrusted user input along with $_GET, $_POST.

The one point of crossover I can think of is an end user attempting to supply a crafted PHPSESSID via $_COOKIE in hopes of gaining access to another user's session.

Unless you directly store other input ($_GET, $_POST, $_COOKIE) into $_SESSION, it can be trusted.

// Oops, $_SESSION['someval'] now holds unfiltered user input!
$_SESSION['someval'] = $_POST['someval'];
云仙小弟 2024-12-29 11:50:45

会话数据不能由用户直接输入,除非您在应用程序中的某个位置创建了此功能;例如:

$_SESSION['lastSearch'] = $_GET['search'];

如果您的应用程序确实具有与此类似的功能,或者您不确定;在将数据打印到页面或在 SQL 语句中使用它之前,我仍然会过滤/清理您的数据。安全总比后悔好。

然而,cookie 数据可以轻松更改,并被视为直接输入。

Session data can't be directly input by the user unless you have created this functionality within your application somewhere; such as:

$_SESSION['lastSearch'] = $_GET['search'];

If your application does have functionality similar to this, or if you aren't sure; I would still filter/sanitize your data becore printing it to the page or using it in a SQL statement. Its better to be safe than sorry.

However, cookie data can be altered easily and is considered direct INPUT.

陌若浮生 2024-12-29 11:50:45

php 有一个名为 $_REQUEST 的全局变量 *默认情况下包含 $_GET、$_POST 和 $_COOKIE 内容的关联数组*

$_REQUEST 中的变量通过 GET、POST 和 COOKIE 输入机制提供给脚本,因此可以由远程用户修改并且不可信。

$_REQUEST 中定义的值由用户在浏览器中定义。

$_SESSION 中的变量不能由浏览器修改。它们仅由php修改,一般可以认为是可信的。

php has a global variable called $_REQUEST *An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE*

The variables in $_REQUEST are provided to the script via the GET, POST, and COOKIE input mechanisms and therefore could be modified by the remote user and cannot be trusted.

The values defined in $_REQUEST are defined by the user, in the browser.

Variables in $_SESSION can't be modified by the browser. They are only modified by php, and generally they can be considered trusted.

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