服务器和执行环境信息

$_SERVER 是一个包含了诸如头信息(header)、路径(path)、以及脚本位置(script locations)等等信息的数组。这个数组中的项目由 Web 服务器创建。不能保证每个服务器都提供全部项目;服务器可能会忽略一些,或者提供一些没有在这里列举出来的项目。这也就意味着大量的此类变量都会在» CGI 1.1 规范中说明,所以应该仔细研究一下。

Note: PHP 5.4.0 之前,$HTTP_SERVER_VARS 包含着相同的信息,但它不是一个超全局变量。 (注意 $HTTP_SERVER_VARS 与 $_SERVER 是不同的变量,PHP处理它们的方式不同)。

1、$_SERVER[PHP_SELF], $_SERVER[SCRIPT_NAME], $_SERVER['REQUEST_URI'] 在用法上是非常相似的,他们返回的都是与当前正在使用的页面地址有关的信息,这里列出一些相关的例子,帮助确定哪些是在你的脚本最适合的。

$_SERVER[’PHP_SELF’]

  • http://www.yoursite.com— – — /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?a=test — – — /example/index.php?a=test
  • http://www.yoursite.com/example/index.php/a/b — – —/example/index.php/a/b

当我们使用$_SERVER['PHP_SELF']的时候,无论访问的URL地址是否有index.php,它都会自动的返回 index.php.但是如果在文件名后面再加斜线的话,就会把后面所有的内容都返回在$_SERVER['PHP_SELF']。

$_SERVER['REQUEST_URI']

  • http://www.yoursite.com— – — /
  • http://www.yoursite.com/example/ — – — /example/
  • http://www.yoursite.com/example/index.php — – — /example/index.php
  • http://www.yoursite.com/example/index.php?a=test — – — /example/index.php?a=test
  • http://www.yoursite.com/example/index.php/a/b— – — /example/index.php/a/b

$_SERVER['REQUEST_URI']返回的是我们在URL里写的精确的地址,如果URL只写到”/”,就返回 “/”

$_SERVER['SCRIPT_NAME']

  • http://www.yoursite.com— – — /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?a=test — – — /example/index.php
  • http://www.yoursite.com/example/index.php/a/b — – — /example/index.php

在所有的返回中都是当前的文件名/example/index.php

猜你喜欢

转载自www.cnblogs.com/samgo/p/8990483.html
今日推荐