$_SERVER is an array containing information such as headers, paths, and script locations. Items in this array are created by the web server. Not every server is guaranteed to provide all items; the server may ignore some, or provide some items not listed here. This also means that a large number of these variables are specified in the » CGI 1.1 specification , so should be studied carefully.
Note: Before PHP 5.4.0, $HTTP_SERVER_VARS contained the same information, but it was not a superglobal variable . (Note that $HTTP_SERVER_VARS and $_SERVER are different variables and PHP handles them differently).
1. $_SERVER[PHP_SELF], $_SERVER[SCRIPT_NAME], $_SERVER['REQUEST_URI'] are very similar in usage, they all return information related to the page address currently in use, here are some related Examples to help determine which ones are the best fit in your script.
$_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
When we use $_SERVER['PHP_SELF'], no matter whether the accessed URL has index.php, it will automatically return index.php. But if you add a slash after the file name, all the following The contents are returned in $_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'] returns the exact address we wrote in the URL, if the URL only writes "/", it returns "/"
$_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
In all returns is the current filename /example/index.php