PHP 获取当前路径、网址 等参数

php | 2019-09-23 13:47:19

测试域名daxiangtravel.com,apache根目录/mnt/,测试目录/mnt/qa/test,测试文件名为index.php

 

php获取当前目录:

getcwd();
// /mnt/qa/test
dirname(__FILE__);
// /mnt/qa/test

 

php获取域名或主机地址

$_SERVER['HTTP_HOST']; 
 //daxiangtravel.com

 

php获取网页地址

$_SERVER['PHP_SELF'];
// /qa/test/index.php

 

php获取网址参数

$_SERVER["QUERY_STRING"];
// v=433

 

php获取用户代理

$_SERVER['HTTP_REFERER'];
// http://daxiangtravel.com/qa/test/index.php?v=433

 

php获取完整的url

'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
// http://daxiangtravel.com/qa/test/index.php?v=433
 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
// http://daxiangtravel.com/qa/test/index.php?v=433

 

php获取路径

$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
echo dirname($url);
// http://daxiangtravel.com/qa/test

 

php获取包含端口号的完整url

'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"]
.$_SERVER["REQUEST_URI"];
// http://daxiangtravel.com:80/qa/test/index.php?v=433


 

登录后即可回复 登录 | 注册
    
关注编程学问公众号