Parse php to get a summary of url and the physical path

  • 2020-06-19 09:51:18
  • OfStack

Here I would like to summarize some information about getting URL in the address bar and the path to which the file belongs:
With $_SERVER[] we can do a lot of things: it's an array of headers (headers), path information, and script locations, and the entities of the array are created by the web server.
This is an "superglobal" or can be described as an automatic global variable. This simply means that it works in all scripts. You don't need to use global $_SERVER in a function or method; Access it as if you were using $HTTP_SERVER_VARS 1.
$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.) If the register_globals directive is set, these variables are also available in all scripts; That is, the $_SERVER and $HTTP_SERVER_VARS arrays are separated.

$_SERVER['HTTP_HOST'] gets the Host: header content for the current request
$_SERVER [' PHP_SELF] it may be our most frequently used, it returns the current call the page file name, if it is http: / / localhost test / 2005 / test php, then will return/test / 2005 / test php
$_SERVER['SCRIPT_NAME'] will return the path containing the current script. This is useful when the page needs to point to itself
$_SERVER['SCRIPT_FILENAME'] returns the absolute path information of the current file
$_SERVER['REQUEST_URI'] returns the URI required to access this page, including "/"

Of course, there are many path functions:
dirname(), returns the directory portion of the path information, preceded by a "/"
basename() returns the basic filename part of the path, but you can also set the suffix to control the output.
realpath(), the absolutely normalized path that returns path information


Related articles: