# 在PHP中使用$_SERVER$_SERVER is one of the PHP global variables—termed Superglobals—which contain information about server and execution environments. These are pre-defined variables so they are always accessible from any class, function or file.

$_SERVER是PHP全局变量之一，称为Superglobals，其中包含有关服务器和执行环境的信息。 这些是预定义的变量，因此始终可以从任何类，函数或文件访问它们。 The entries here are recognized by web servers, but there is no guarantee that each web server recognizes every Superglobal. These three PHP$_SERVER arrays all behave in similar ways—they return information about the file in use. When exposed to different scenarios, in some cases they behave differently. These examples may help you decide which is best for what you need. A full list of $_SERVER arrays is available at the PHP website. Web服务器可以识别此处的条目，但不能保证每个Web服务器都可以识别每个Superglobal。 这三个PHP$ _SERVER数组的行为均相似，它们返回有关正在使用的文件的信息。 在不同情况下，它们在某些情况下的行为会有所不同。 这些示例可以帮助您确定最适合您的需求的示例。 $_SERVER数组的完整列表可在PHP 网站上找到 ##$ _SERVER ['PHP_SELF'] ( $_SERVER['PHP_SELF'] ) PHP_SELF is the name of the currently executing script. PHP_SELF是当前正在执行的脚本的名称。 • http://www.yoursite.com/example/ -- --> /example/index.php http://www.yoursite.com/example/--> /example/index.php • http://www.yoursite.com/example/index.php -- --> /example/index.php http://www.yoursite.com/example/index.php--> /example/index.php • http://www.yoursite.com/example/index.php?a=test -- --> /example/index.php http://www.yoursite.com/example/index.php?a=test--> /example/index.php • http://www.yoursite.com/example/index.php/dir/test -- --> /dir/test http://www.yoursite.com/example/index.php/dir/test--> / dir / test When you use$_SERVER[’PHP_SELF’], it returns the file name /example/index.php both with and without the file name typed in the URL. When variables are appended at the end, they were truncated and again /example/index.php was returned. The only version that produced a different result has directories appended after the file name. In that case, it returned those directories.

SCRIPT_NAME is the current script's path. This comes in handy for pages that need to point to themselves.

SCRIPT_NAME是当前脚本的路径。 这对于需要指向自己的页面非常有用。

• http://www.yoursite.com/example/ -- --> /example/index.php

http://www.yoursite.com/example/--> /example/index.php

• http://www.yoursite.com/example/index.php -- --> /example/index.php

http://www.yoursite.com/example/index.php--> /example/index.php

• http://www.yoursite.com/example/index.php?a=test -- --> /example/index.php

http://www.yoursite.com/example/index.php?a=test--> /example/index.php

• http://www.yoursite.com/example/index.php/dir/test -- --> /example/index.php

http://www.yoursite.com/example/index.php/dir/test--> /example/index.php

All cases here returned only the file name /example/index.php regardless of whether it was typed, not typed, or anything was appended to it.

• 0
点赞
• 0
评论
• 0
收藏
• 扫一扫，分享海报

12-08 16