php中这些东西是什么?例如:$_COOKIE[]、$_POST[]等

发布于 2024-12-22 11:23:52 字数 693 浏览 0 评论 0原文

我刚刚开始,所以如果这是一个愚蠢的问题,请原谅。

我正在通过阅读书籍和观看可能稍微过时的视频系列来学习。

在涉及多个页面的某些情况下给出的说明使用常规变量语法,但除非我使用本文标题中提到的语法,否则该语法不起作用。

视频中的示例包括:

第 1 页:

<?php

setcookie("color","blue");

?>

第 2 页:

<?php

echo $color;

?>

这对我不起作用。但是,如果我使用其他语法更改内容,它确实有效。

变量语法更改使其起作用的示例:

第 1 页:

<?php

setcookie("color","blue");

?>

第 2 页:

<?php

echo $_COOKIE["color"];

?>

所以我的问题是,我正在使用的语法(例如:$_COOKIE["color"])是什么?它叫什么名字,它的工作原理是什么?

我意识到这与会话有关,但我太新手,无法理解除此之外的内容。

感谢您的帮助!

I'm just starting out, so please forgive if this is a silly question.

I'm learning by reading a book and watching a video series that may be slightly out-dated.

The instructions given in certain cases where more than one page is involved use regular variable syntax, but that syntax does not work unless I use the syntax mentioned in the title of this post.

Example from the video includes:

Page 1:

<?php

setcookie("color","blue");

?>

Page 2:

<?php

echo $color;

?>

This does not work for me. However, if I change things using the other syntax, it does work.

Example from changes to variable syntax that makes it work:

Page 1:

<?php

setcookie("color","blue");

?>

Page 2:

<?php

echo $_COOKIE["color"];

?>

So my question is, what is this syntax (ex: $_COOKIE["color"]) that I am using? What is it called and what/how does it work exactly?

I realize this has something to do with sessions, but am too novice to understand beyond that.

Thank you for your help!

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

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

发布评论

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

评论(4

來不及說愛妳 2024-12-29 11:23:52

它们被称为超级全局变量,它们是始终被定义的“特殊”变量。

http://php.net/manual/en/language.variables.superglobals.php

如果启用了邪恶的 register_globals,第一个示例就可以工作:

http://php.net/manual/en/security.registerglobals.php

如果您正在从使用 register_globals 的源学习 PHP,我建议您寻找另一个源。

They are called Superglobals, they are "special" variables that are always defined.

http://php.net/manual/en/language.variables.superglobals.php

The first example can work if the evil, evil register_globals is enabled:

http://php.net/manual/en/security.registerglobals.php

If you are learning PHP from a source that is using register_globals, I suggest you find another source.

农村范ル 2024-12-29 11:23:52

第一种语法,即“查找已经存在的变量”,依赖于名为 注册全局变量。这个功能很早以前就被引入了,是为了方便新手,但它一直是许多安全问题的根源,并且已经被弃用了一段时间(事实上,对这种模式的支持已经完全停止)在即将发布的 PHP 5.4 版本中删除)。要获得快速概述,请阅读“register_globals 对我有何影响?”在常见问题解答中。

访问“环境”变量(即源自当前 HTTP 请求或服务器设置的变量)的另一种方法是访问 超级全局 变量;该页面也链接到上面的寄存器全局部分。

如果您正在阅读依赖于寄存器全局变量处于活动状态的教程,您应该立即停止阅读它们并找到更好、更新的材料。

The first type of syntax, where you just "find the variables already there" is dependent on a PHP feature called register globals. This feature was introduced a long time ago as a convenience to novices, but it has been the source of many security issues and has been deprecated for some time now (in fact, support for this mode has been totally removed in the upcoming version PHP 5.4). For a very quick overview, read "how does register_globals affect me?" in the FAQ.

The other way of accessing "environment" variables (i.e. those originating from the current HTTP request or the server setup) is by accessing the superglobal variables; this page is also linked to from the register globals piece above.

If you are reading tutorials that depend on register globals being active you should immediately stop reading them and find better and more recent material.

终陌 2024-12-29 11:23:52

是的,该示例不起作用,因为它已经过时了。在 PHP 中,有一个名为 register_globals 这使得该功能有效,但不建议使用它。

您正在讨论的语法 ($_COOKIE["color"]) 是获取 (关联)数组$_COOKIE 本身是一个 < a href="http://php.net/manual/en/language.variables.superglobals.php" rel="nofollow">superglobal 数组,它总是存储包含在HTTP 请求。

会话是另外一回事,cookie 可以用来维持会话。

Yes, the example does not work because it is outdated. In PHP, there is a setting called register_globals which makes this feature work, but it is not recommended to use it.

The syntax you are talking about ($_COOKIE["color"]) is the syntax to get the element of an (associative) array. $_COOKIE itself is a superglobal array, and it always stores the values of cookies that were included in the HTTP request.

Sessions is something else, cookies can be used to maintain a session.

以歌曲疗慰 2024-12-29 11:23:52

这些称为超级全局。它们是在任何地方都可用的全局数组,无需使用 global() 显式定义它们。有几个可用的,用于 cookie、上传的文件、POST 变量等。

有关更多信息,最好参考 PHP 手册: http://php.net/superglobals

These are called Superglobals. They are global arrays that are available anywhere, without explicitly defining them using global(). There are sevearl available, for cookies, uploaded files, POST variables, etc.

For more information, it's probably best to refer to the PHP manual: http://php.net/superglobals.

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