Home > Article > Backend Development > Summary of obtaining url and physical path in parsing php_PHP tutorial
Here I would like to summarize some information about getting the URL in the address bar and the path to which the file belongs:
Using $_SERVER[ ] we can accomplish many things Thing: It is an array containing headers, path information and script location. The entities of the array are created by the web server.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts. You do not need to use global $_SERVER; to access it within a function or method, as you would with $HTTP_SERVER_VARS.
$HTTP_SERVER_VARS contains the same information, but is not an automatic global variable. (Note: $HTTP_SERVER_VARS and $_SERVER are different variables, and PHP handles them differently.) These variables are also available in all scripts if the register_globals directive is set; that is, the $_SERVER and $HTTP_SERVER_VARS arrays are separated .
$_SERVER['HTTP_HOST'] Gets the Host: header content of the current request
$_SERVER['PHP_SELF'] This may be the one we use most often. It returns the information of the currently called page. The file name, if it is http://localhost/test/2005/test.php, will return /test/2005/test.php
$_SERVER['SCRIPT_NAME'] which will return the path containing the current script. This is useful when the page needs to point to itself
$_SERVER['SCRIPT_FILENAME'] It will return the absolute path information where the current file is located
$_SERVER['REQUEST_URI'] Returns the URI required to access this page, including " /"
Of course there are many path functions:
dirname(), which returns the directory part of the path information, preceded by
basename( with "/" ) returns the basic file name part of the path. Of course, the suffix can also be set to control the output.
realpath(), returns the absolutely normalized path of the path information