Home  >  Article  >  php教程  >  PHP中获取URL完整路径程序代码

PHP中获取URL完整路径程序代码

WBOY
WBOYOriginal
2016-05-25 16:43:541314browse

在php中有系统自带的全局变量$_SERVER,里面有很多参数,下面我来介绍获取URL完整路径程序代码有需要的朋友可参.

举例 URL : http://www.phprm.com/Test/Test.php,代码如下:

$_SERVER['SERVER_NAME']:www.phprm.com 
	$_SERVER['SERVER_PORT']:80 
	$_SERVER['REQUEST_URI']:/Test/Test.php 

那我们把上面几句结合起来就可以实现了,URL完整路径为:

“http://”.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] 

如果你是开了端口号,我们可以使用$_SERVER['SERVER_PORT']就好了,上面的代码修改一下即可,代码如下:

“http://”.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'] 

大家要注意一点:

$_SERVER['PHP_SELFT']与$_SERVER['SCRIPT_NAME']和$_SERVER['REQUEST_URI']和$_SERVER['QUERY_STRING']的区别:

$_SERVER['PHP_SELF']会返回/phptour/test7.php/a=b,会返回文件名/后面的内容

$_SERVER['SCRIPT_NAME']只会返回/phptour/test.php,不会返回文件名后面的/后面的内容

http://localhost:60/phptour/test/test7.php/a=b

$_SERVER['PHP_SELF']将得到:/phptour/test/test7.php/a=b

$_SERVER['SCRIPT_NAME']将得到:/phptour/test/test7.php

$_SERVER['REQUEST_URI']将得到:/phptour/test/test7.php/a=b

$_SERVER['QUERY_STRING']将得到:''

Statement:
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