PHP $_SERVER['PHP_SELF']、$_SERVER['SCRIPT_NAME'] 与 $_SERVER['REQUEST_URI'] 之间的区别

=============================================================================================================
=============================================================================================================
=============================================================================================================
=============================================================================================================
=============================================================================================================

The CGI (Common Gateway Interface) defines a way for a web server to interact with external content-generating programs,
which are often referred to as CGI programs or CGI scripts. It is a simple way to put dynamic content on your web site,
using whatever programming language you're most familiar with. This document will be an introduction to setting up CGI on
your Apache web server, and getting started writing CGI programs.
这句话就比较难理解, CGI 是一个规范,初步理解是: apache 怎么把数据传出去, 然后接收什么样的数据, 
那: 符合cgi标准, 能通过cgi的方式和apache 交流的, 就是cgi程序或者cgi脚本, 这个完全是站在apache的角度来说的,
这个命名好像不是好记,

就是说 cgi 是 apache 里面一个小的功能, 要开放了激活了才能用。
LoadModule cgid_module modules/mod_cgid.so
LoadModule cgi_module modules/mod_cgi.so


=============================================================================================================


http://www.5idev.com/php/index.php/test/foo?username=hbolive
  • $_SERVER['PHP_SELF'] 得到:/php/index.php/test/foo
  • $_SERVER['SCRIPT_NAME'] 得到:/php/index.php
  • $_SERVER['REQUEST_URI'] 得到:/php/index.php/test/foo?username=hbolive

从该例子可以看出:

  1. $_SERVER['PHP_SELF'] 则反映的是 PHP 程序本身;
  2. $_SERVER['SCRIPT_NAME'] 反映的是程序文件本身(这在页面需要指向自己时非常有用);
  3. $_SERVER['REQUEST_URI'] 则反映了完整 URL 地址(不包括主机名)。

其实从各自的命名上,也可以体现出它们之间的细微差别。

特别的,对于如下地址:

http://www.5idev.com/
  • $_SERVER['PHP_SELF'] 得到:/index.php
  • $_SERVER['SCRIPT_NAME'] 得到:/index.php
  • $_SERVER['REQUEST_URI'] 得到:/

至于有人提到 $_SERVER['PHP_SELF'] 与 $_SERVER['SCRIPT_NAME'] 在 PHP 以 CGI 模式运行下会有区别。由于 PHP 在 CGI 模式运行下并不多见,测试较为麻烦,在此就不再叙述了。如有这种情况,可注意并自行测试。

 

http://www.5idev.com/p-php_server_php_self_script_name_request_uri.shtml

posted @ 2016-07-25 12:55  qqisnow2021  阅读(166)  评论(0编辑  收藏  举报