Home > Article > Backend Development > PHP gets the full path and physical path of the URL_PHP tutorial
PHP’s predefined variable $_SERVER is an array containing headers, path information and script location. The entities of the array are created by the web server.
Using the $_SERVER array, we can get the full path and real path of the URL, allowing us to use them as we wish.
$_SERVER['SERVER_NAME'] // The name of the server host where the script is currently running.
$_SERVER[SERVER_PORT'] //The port used by users to connect to the server.
$_SERVER['REQUEST_URI'] //The request URL of the currently running script except the host name.
$_SERVER['DOCUMENT_ROOT'] //The document root directory where the currently running script is located.
Example URL: http://www.cndong.cn/Test/Test.php
$_SERVER['SERVER_NAME']: www.cndong.cn
$_SERVER['SERVER_PORT'] : 80www.2cto.com
$_SERVER['REQUEST_URI'] : /Test/Test.php
$_SERVER['DOCUMENT_ROOT'] : /home/var/www/cndong.cn/htdocs
So the full URL path is: “http://”.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']
Description:
1. The default http port is 80, so generally $_SERVER['SERVER_PORT'] does not include the full path of the URL. Of course, special cases will be treated specially!
2. Some people will use $_SERVER['REMOTE_HOST'] to get the host name, but its meaning is: the host name of the user who is browsing the current page (this variable must be created by the web server).
Extension:
Solution to the null value of $_SERVER['REMOTE_HOST']
Modify the HostnameLookups in the apache server configuration file httpd.conf to On
Author: Xu Xiujun