PHP $_SERVER variables
Blogs20112011-02-17
To refer the script itself, we have 4 optional variables:
- $_SERVER[âPHP_SELFâ]
- $_SERVER[âSCRIPT_NAMEâ]
- $_SERVER[âREQUEST_URIâ]
- $_SERVER[HTTP_REFERER]
The first three are exactly the same, they are all relative to URL, represent the current running script file. The last one, $_SERVER[HTTP_REFERER], normally used in a parent-child/context env (frameset, <formâŠ>) to refer to parentâs script name.
When I run phpinfo.php in my pcâs xampp env, they look like the following:
- $_SERVER[âREQUEST_URIâ]: /xampp/phpinfo.php
- $_SERVER[âSCRIPT_NAMEâ]: /xampp/phpinfo.php
- $_SERVER[âPHP_SELFâ]: /xampp/phpinfo.php
- $_SERVER[âHTTP_REFERERâ]: http://localhost/xampp/navi.php
$_SERVER[âPHP_SELFâ] and $_SERVER[âSCRIPT_NAMEâ] reprent script name, without passed parameters; On the other side, $_SERVER[âREQUEST_URIâ] will be with the parameters, e.g: /example/index.php?a=test.
Normally I use largely $_SERVER[âPHP_SELFâ] in my codes to call the default php script; and $_SERVER[âHTTP_REFERERâ] to refer to parentâs script.
A good reference is at: http://php.about.com/od/learnphp/qt/\_SERVER\_PHP.htm
