user2757460
user2757460

Reputation: 11

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)

Upvotes: 1

Views: 7411

Answers (2)

mmr
mmr

Reputation: 516

call this function and it will return the 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;
}

Upvotes: 2

Giacomo1968
Giacomo1968

Reputation: 26066

You have to do some coding to get it to work with 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);

Upvotes: 1

Related Questions