Home > php教程 > php手册 > PHP获取URL完整路径及物理路径

PHP获取URL完整路径及物理路径

WBOY
Release: 2016-06-13 10:50:06
Original
1576 people have browsed it

PHP的预定义变量$_SERVER是一个包含头部(headers),路径信息及脚本位置的数组,数组的实体由web服务器创建.
利用$_SERVER数组,我们可以获取URL完整路径及真实路径,让自己随心所欲的运用它们.
$_SERVER['SERVER_NAME'] // 当前运行脚本所在服务器主机的名称.
$_SERVER[SERVER_PORT'] //用户连接到服务器时所使用的端口.
 $_SERVER['REQUEST_URI'] //当前运行脚本除主机名外的请求URL.
$_SERVER['DOCUMENT_ROOT'] //当前运行脚本所在的文档根目录.
举例 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
所以URL完整路径为: “http://”.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']
说明 :
  1.  默认http的端口为80 ,所以一般$_SERVER['SERVER_PORT']不纳入URL完整路径,当然特殊情况,特殊对待!
   2. 有人会用$_SERVER['REMOTE_HOST']获取主机名,可是它的含义是:正在浏览当前页面用户的主机名(此变量必须由Web 服务器来创建).
延伸:
$_SERVER['REMOTE_HOST']为空值的解决方法
修改apache服务器配置文件httpd.conf内的HostnameLookups为On

 作者:徐修俊

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template