PHP获取当前url路径的函数及服务器变量

$_SERVER["QUERY_STRING"],

$_SERVER["REQUEST_URI"],

$_SERVER["SCRIPT_NAME"],

$_SERVER["PHP_SELF"]


 

请求地址:http://127.0.0.1/demo.php/demo/?name=tom

SCRIPT_NAME: /demo.php/demo/index.php
PHP_SELF:/demo.php/demo/index.php
REQUEST_URI:/demo.php/demo/?name=tom
QUERY_STRING:name=tom
SCRIPT_FILENAME:D:/phpstudy2018/PHPTutorial/WWW/demo.php/demo/index.php

1,$_SERVER["QUERY_STRING"]
说明:查询(query)的字符串
 
2,$_SERVER["REQUEST_URI"]
说明:访问此页面所需的URI
 
3,$_SERVER["SCRIPT_NAME"]
说明:包含当前脚本的路径
 
4,$_SERVER["PHP_SELF"]
说明:当前正在执行脚本的文件名

 


 

实例:
1,http://www.biuuu.com/ (直接打开主页)
结果:
$_SERVER["QUERY_STRING"] = ""
$_SERVER["REQUEST_URI"] = "/"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"]     = "/index.php"

 

2,http://www.biuuu.com/?p=222 (附带查询)
结果:
$_SERVER["QUERY_STRING"] = "p=222"
$_SERVER["REQUEST_URI"] = "/?p=222"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"]     = "/index.php"

 

3,http://www.biuuu.com/index.php?p=222&q=biuuu
结果:
$_SERVER["QUERY_STRING"] = "p=222&q=biuuu"
$_SERVER["REQUEST_URI"] = "/index.php?p=222&q=biuuu"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"]     = "/index.php"

 

4,http://www.5idev.com/php/index.php/test/foo?username=hbolive

结果:
$_SERVER['PHP_SELF'] 得到:/php/index.php/test/foo
$_SERVER['SCRIPT_NAME'] 得到:/php/index.php
$_SERVER['REQUEST_URI'] 得到:/php/index.php/test/foo?username=hbolive

 

$_SERVER["QUERY_STRING"]获取查询语句,实例中可知,获取的是?后面的值
$_SERVER["REQUEST_URI"] 获取http://www.biuuu.com后面的值,包括/
$_SERVER["SCRIPT_NAME"] 获取当前脚本的路径,如:index.php
$_SERVER["PHP_SELF"] 当前正在执行脚本的文件名
 
当前url:"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']
 
总结一下,
对于QUERY_STRING,REQUEST_URI,SCRIPT_NAME和PHP_SELF,深入了解将有利于我们在$_SERVER函数中正确调用这四个值。通过实例详解$_SERVER函数中QUERY_STRING,REQUEST_URI,SCRIPT_NAME和PHP_SELF掌握四个变量之间的区别。
 
posted @ 2019-04-12 11:55  白開水  阅读(3055)  评论(0编辑  收藏  举报