简体   繁体   English

如何在本地主机中获取本地主机/文件夹名称URL

[英]How to get localhost/foldername url in localhost

I setup folder in localhost. 我在本地主机中设置文件夹。 I want to write a script to get that domain path but I've tried using 我想编写一个脚本来获取该域路径,但是我尝试使用

$_SERVER['SERVER_NAME'] 
$_SERVER['HTTP_HOST'] 
getenv('HTTP_HOST')

these all given http://localhost only , how could i get full path like http://localhost/abc (abc is a folder, in www directory) 这些都只给了http://localhost ,我怎么能得到像http://localhost/abc这样的完整路径(abc是www目录中的一个文件夹)

call this function and it will return the url 调用此函数,它将返回URL

public function curPageURL() 
{
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") 
    {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } 
    else 
    {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

You have to do some coding to get it to work with parse_url() . 您必须进行一些编码才能使其与parse_url()一起使用 So I would suggest this: 所以我建议:

$parsed_url = parse_url($_SERVER['PHP_SELF']);
echo $parsed_url['path'];

But that would show the full path including the script name so you might have to add some logic to parse further. 但这将显示包括脚本名称在内的完整路径,因此您可能必须添加一些逻辑以进一步解析。

EDIT: Decided to do some more coding on this concept & here is the result. 编辑:决定对此概念进行更多编码,这就是结果。 Will return the path of the current script but without the script filename: 将返回当前脚本的路径,但不包含脚本文件名:

$parsed_url = parse_url($_SERVER['PHP_SELF']);
$path_array = explode('/', $parsed_url['path']);
array_pop($path_array);
echo join('/', $path_array);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM