php的web路径获取

<?php
class HttpTool
{
	/**
	 * //获取域名或主机地址 
	 * #测试网址:     http://localhost:8081/test/testurl.php?id=5
	 * 返回  localhost:8081
	 */
	public function getHost()
	{
		return $_SERVER['HTTP_HOST'];
	}
	
	/**
	 * 当前页面的url(包括参数)
	 */
	public function getWebUrl()
	{
		$pageURL = 'http';
		if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
		{
			$pageURL .= "s";
		}
		$pageURL .= "://";
		$pageURL .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		return $pageURL;
	}
	
	/**
	 * 
	 * 当前页面的url(不包括参数)
	 */
	public function getWebPath()
	{
		$pageURL = 'http';
		if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
		{
			$pageURL .= "s";
		}
		$pageURL .= "://";
		$pageURL .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
		return $pageURL;
	}
	
	/**
	 * 当前页面的父路径
	 */
	public function getWebParentPath()
	{
		$pageURL = 'http';
		if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
		{
			$pageURL .= "s";
		}
		$pageURL .= "://";
		$pageURL .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
		$pageURL = substr($pageURL, 0, strrpos($pageURL, "/"));
		return $pageURL;
	}
	
	/**
	 * 服务器名称
	 */
	public function getServerName()
	{
		return $_SERVER['SERVER_NAME'];
	}
	
	/**
	 * 端口
	 */
	public function getServerPort()
	{
		return $_SERVER["SERVER_PORT"];
	}
	
	/**
	 * 链接参数,问号?后的参数
	 */
	public function getQueryString()
	{
		return $_SERVER['QUERY_STRING'];
	}
	
	/**
	 * 请求地址,返回值不host内容
	 */
	public function getRequestUri()
	{
		return $_SERVER['REQUEST_URI'];
	}
}

$http = new HttpTool();
echo "host===============".$http->getHost() . "<br/>";
echo "weburl=============".$http->getWebUrl() . "<br/>";
echo "webPath============".$http->getWebPath() . "<br/>";
echo "getWebParentPath===".$http->getWebParentPath() . "<br/>";
echo "getServerName======".$http->getServerName() . "<br/>";
echo "getServerPort======".$http->getServerPort() . "<br/>";
echo "getQueryString=====".$http->getQueryString() . "<br/>";
echo "getRequestUri======".$http->getRequestUri() . "<br/>";

?>


测试地址:http://localhost:8081/test/httptool.php?name=penngo

输出结果:

host===============localhost:8081
weburl=============http://localhost:8081/test/httptool.php?name=penngo
webPath============http://localhost:8081/test/httptool.php
getWebParentPath===http://localhost:8081/test
getServerName======localhost
getServerPort======8081
getQueryString=====name=penngo
getRequestUri======/test/httptool.php?name=penngo




转载于:https://my.oschina.net/penngo/blog/112893

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值