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

后端 未结 6 781
眼角桃花
眼角桃花 2020-12-09 09:06

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

6条回答
  •  [愿得一人]
    2020-12-09 10:06

    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']

提交回复
热议问题