Pergunta

Dn't know if this is at all possible - but let me explain my problem.

I'm working on a project with multiple developers. We all have our own development environment set up. For some - the website we're building is located in one or more subfolders of their document root - for example

/library/webserver/htdocs/project/website

Others have a different path the the site, like

/htdocs/website

When it comes to finding a common way to get the document root (to include php files and such) it no problem. But I'd like to also find the website root URL, meaning in the first case:

http://localhost/project/website/

and in the second case

http://localhost/website/

In production, the same method should return simply

http://domain.tld.

Is there a nice way to do this - without having to define paths manually? Thank you!

Foi útil?

Solução

I ended up with a function looking like this. It uses the document root for the current server, but then strips away the part before directory name of the website root - which is really the directory name of the directory name one step above were this function is located:

$dirName = dirname(dirname(__FILE__) . ".." . DIRECTORY_SEPARATOR);
$dirName = str_replace('\\', "/", $dirName);    
$base = str_replace($_SERVER['DOCUMENT_ROOT'], "", $dirName);

With a document root of /etc/webserver/documents/ and a website under /projects/website1 in your document root it would return /projects/website1 if this script is located in /projects/website1/somreDir/someFile.php.

Hopes this helps someone.

Outras dicas

I think you're looking for $_SERVER['HTTP_ROOT'] :

'HTTP_HOST'

Contents of the Host: header from the current request, if there is one.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top