关于$_SERVER中的PHP_SELF、REQUEST_URI以及SCRIPT_NAME的区别

文章主要引用于:https://www.cnblogs.com/zcy_soft/archive/2010/10/16/1853239.html

1.实验环境:需要开启appache的rewrite功能,在工程下创建index.php文件,然后在该工程下增加.htaccess文件——以单一入口文件形式访问。

2.说明:现在很多站点都会引用单一入口原则,所以这是在单一入口基础上得到的结果。

其中以四种不同的URL形式来访问

http://www.yoursite.com/example/ 

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

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

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

以下为几种不同结果和引用文得到的结果不同这是实验环境不同不同所致的,请不要说谁错了。引文其实也没错只是没有说是什么环境下可以得那样的结果。好了直接给结果

3.结果:

$_SERVER[’PHP_SELF’]

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

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

$_SERVER['REQUEST_URI']

  • 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/dir/test — – — /example/index.php/dir/test

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

$_SERVER['SCRIPT_NAME']

  • http://www.yoursite.com/example/ — – — /index.php
  • http://www.yoursite.com/example/index.php — – — /index.php
  • http://www.yoursite.com/example/index.php — – — /index.php
  • http://www.yoursite.com/example/index.php/dir/test — – — /index.php

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

4总结:在以上结果中REQUEST_URI是 HOST后面的内容。SCRIPT_NAME总是访问的文件(再者即index.php),PHP_SELF则执行文件后面再接上URL路径。

你可能感兴趣的:(Php)