PHP获取当前主机、域名、网址、路径、端口等参数

获取当前目录:

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

获取域名或主机地址

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

获取网页地址

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

获取网址参数

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

获取用户代理

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

获取完整的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

只取路径

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

包含端口号的完整url

 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"]
.$_SERVER["REQUEST_URI"];
// http://daxiangtravel.com:80/qa/test/index.php?v=433
THE END
喜欢就支持一下吧
点赞9 分享