Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the "application root" of my URL from PHP?

Tags:

If my URL is http://www.server.com/myapp/stuff/to/see and my directory structure on disk is htdocs/myapp/*, how can I extract the /myapp part? Also, what if there was no application folder and the application root was just '/'? Is there a predefined variable for getting that value?

What I want is a function that is able to trim /myapp off of the request URI, so that I'm only left with /stuff/to/see. AND, if I were to move the application to the server document root, have it determine that as well (so that /stuff/to/see is just returned as /stuff/to/see)

My directory structure is this:

application   |-config     |-config.inc.php library   |-autoload.class.php public   |-css   |-img index.php 

So, from index.php, I want to know how to get what I asked above.

like image 738
scottm Avatar asked Feb 22 '10 15:02

scottm


People also ask

How can I get root URL in PHP?

$pathInPieces = explode('/', $_SERVER['DOCUMENT_ROOT']); echo $pathInPieces[0]; This will output the server's root directory.

How do I find application root path?

To simply access the app's root path, use the module as though it were a string: var appRoot = require('app-root-path'); var myModule = require(appRoot + '/lib/my-module. js'); Side note: the module actually returns an object, but that object implements the toString method, so you can use it as though it were a string.

How do I change the root path in PHP?

The chroot() function changes the root directory of the current process to directory, and changes the current working directory to "/".


2 Answers

When you access a page using http://www.server.com/myapp/stuff/to/see, and your webserver's document root is at /something/htdocs/, then the current local path is /something/htdocs/myapp/stuff/to/see.

There is no definite solution to get /something/htdocs/myapp/ as a result now, because there is no clear definition which path you should get. When you have some rule for that, tell us, and we might be able to come up with something, but without computation, the only paths you can see are:

  • http://www.server.com/myapp/stuff/to/see
    $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
  • /something/htdocs/myapp/stuff/to/see/index.php
    $_SERVER['SCRIPT_FILENAME']
  • /something/htdocs/
    $_SERVER['DOCUMENT_ROOT']
  • /myapp/stuff/to/see
    $_SERVER['REQUEST_URI']
  • /myapp/stuff/to/see/index.php
    $_SERVER['SCRIPT_NAME'] and $_SERVER['PHP_SELF']
like image 98
poke Avatar answered Oct 26 '22 22:10

poke


For the web root, there is DOCUMENT_ROOT as pointed out in several answers. For the application root, there is no way for the system to tell which folder in your application is the root folder. You will have to set that manually.

Most applications define an application root using dirname(__FILE__); or a config setting and store that in a constant that is available throughout the application.

like image 32
Pekka Avatar answered Oct 26 '22 22:10

Pekka