Portekoi
Portekoi

Reputation: 1157

Get path folder in URL with PHP

I use this PHP code for find the url path of a script page :

define("URL", dirname($_SERVER['REQUEST_URI']));

On my PC 1, I get this result with this url : htt://site1/abc/ :

/abc

but on my PC 2, I get this result with the same URL :

\

Don't understand why. If i add 'index.php', that's ok on PC 2

My goal is create a wizard installation and put the url path in the config file.

Upvotes: 1

Views: 104

Answers (1)

Gergo Erdosi
Gergo Erdosi

Reputation: 42083

You may get unexpected results with dirname(), see the example in the manual:

echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or \ on Windows)

Use parse_url() instead:

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = '/'.implode('/', explode('/', ltrim($path, '/'), -1));
define("URL", $path);

Upvotes: 1

Related Questions