在PHP中使用$_服务器

$_SERVER是称为Superglobals的PHP全局变量之一,其中包含有关服务器和执行环境的信息。这些是预定义的变量,因此始终可以从任何类,函数或文件中访问它们。

这里的条目被web服务器识别,但不能保证每个web服务器都能识别每个超级全局。这三个PHP$U服务器阵列的行为方式都相似-它们返回有关使用中文件的信息。当暴露于不同情况时,在某些情况下它们的行为会有所不同。这些例子可能有助于您决定哪一个最适合您需要。PHP网站上提供了$U服务器阵列的完整列表。

$_服务器['PHP_SELF']

PHP_SELF是当前执行脚本的名称。

  • http://www.yoursite.com健康知识网/example/--->/示例/index.php
  • http://www.yoursite.com/example/index.php--->/示例/index.php
  • http://www.yoursite.com/example/index.php?a=测试->/示例/index.php
  • http://www.yoursite.com/example/index.php/dir/test--->/dir/test

当你使用$u服务器['PHP u SELF']时,它返回文件名/示例/index.php无论是否在URL中键入文件名。当变量在末尾附加时,它们被截断并再次/示例/index.php被退回了。**产生不同结果的版本在文件名后面附加了目录。在这种情况下,它会返回这些目录。

$U服务器['请求UURI']

请求URI是指用于访问页面的URI。

  • http://www.yoursite.com/example/--->/
  • http://www.yoursite.com/example/index.php--->/示例/index.php
  • http://www.yoursite.com/example/index.php?a=测试->/示例/index.php?a=test
  • http://www.yoursite.com/example/index。php/dir/test-->/示例/index.php/dir/test

所有这些示例都返回了URL输入的内容。它返回一个普通的/,文件名,变量和附加的目录,就像它们被输入一样。

$U服务器['脚本U名称']

脚本名称是当前脚本's路径。这对于需要指向自己的页面很有用。

  • http://www.yoursite.com/example/--->/示例/index.php
  • http://www.yoursite.com/example/index.php--->/示例/index.php
  • http://www.yoursite.com/example/index.php?a=测试->/示例/index.php
  • http://www.yoursite.com/example/index.php/dir/test--->/示例/index.php

这里的所有案例只返回文件名/示例/index.php无论是键入,未键入还是附加任何内容。