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

后端 未结 6 812
眼角桃花
眼角桃花 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 09:48

    $_SERVER['DOCUMENT_ROOT'] will give you the path to htdocs, then you can go from there

    EDIT

    $_SERVER['REQUEST_URI'] will always give you /myapp/stuff/to/see from your sample url above, regardless of the files location on disk or from which file it is invoked.

    so it only a matter of explodeing, array_shifting, and implodeing.

    btw, from your directory structure, it looks like you're using a framework. some frameworks have a URI/URL library that you can find useful (at least CodeIgniter has, not 100% sure with other frameworks)

提交回复
热议问题