phpのweb経路取得

2925 ワード

<?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========local host:8081 weburl==============================http://localhost:8081/test/httptool.php?name=penngo webPath=======================http://localhost:8081/test/httptool.php get WebPant===http://localhost:8081/test get ServerName====local Host get ServerPort===8081 get QueryString==name=penngo get Request Uri========test/httool.php?name=penngo