douliang4858 2018-02-05 19:23
浏览 55
已采纳

查找PHP应用程序的安装目录

PHP $_SERVER['...'] variables such as for 'PHP_SELF', 'SCRIPT_NAME', 'REQUEST_URI' have me somewhat confused. I've seen (at least some) previous questions about these and can do some testing in some environments, but I've run into environments where unexpected things happen.

I write apps that must be installable in any subdirectory below the document root. For example, suppose that the document root is /var/www/html and that the app is installed in /var/www/html/sub/dir . I need to require() files in /var/www/html/sub/dir (or subdirectories to that) and link to files in www.example.com://sub/dir . Call '/sub/dir' SUB_DIR. I also want rewrite rules for friendly urls such as www.example.com/sub/dir/route/key1/val1/key2/val2 .

I have an .htaccess for Apache and a web.config for IIS to do my rewrites to send everything through /var/www/html/sub/dir/index.php. I have not run into a need to figure out nginx or others.

I've been doing something like following but with more care about off by one issues and about Windows v. real world directory separators.

$docRoot = $_SERVER['DOCUMENT_ROOT'];
$reqUrl = $_SERVER['REQUEST_URI'];
$pwd = __DIR__;  /* Occuring in www.example.com/sub/dir/index.php) */
/* Expected example value for $pwd: /var/www/html/sub/dir */
$appLinkDir = substr($pwd, strlen(docRoot));
/* Expected example value for $appLinkDir: /sub/dir */
$workingRequest = substr($_SERVER['REQUEST_URI'], strlen(appLinkDir));
/* Expected example value for $workingRequest: /route/key1/val1/key2/val2 */

I ran into an environment where $pwd is /var/www/html instead of /var/www/html/sub/dir . My guess is that the app was installed in /var/www/html (not /var/www/html/sub/dir) but was they were doing global rewrites from /sub/dir/index.php (or something). At any rate, the unexpected value of $pwd gave me unexpected values for $appLinkDir and $workingRequest .

The problematic environment was from a third party code reviewer who may have assumed my app expected to be installed in /var/www/html/sub/dir when it in fact was expecting to discover where it was installed.

Any good (or even bad) ideas on how I can get correct values for $appLinkDir and $workingRequest ?

  • 写回答

1条回答 默认 最新

  • douyi02577 2018-02-05 20:46
    关注

    __DIR__ gives you the directory path where the php file is located on disk.

    It won't change unless you move the php file on disk.

    So if the file is placed under /var/html/sub/another/dir/index.php __DIR__ would be /var/html/sub/another/dir/.

    Always use __DIR__ for including other php resources.

    Now for the URL rewriting part:

    I ran in a similar problem and what I've done is just passing the whole request URI to my php script and do the parsing there.

    For example:

    <?php
    echo $_SERVER['REQUEST_URI'];
    

    With a rewrite (for lighttpd as a reference):

    url.rewrite-once = (
        # I don't want to route PNG, CSS files with PHP..
        "^/_assets/(.*)$" => "/_assets/$1", 
        "/(.*)$" => "index.php?$1"
    )
    

    If you access /test/blub/blab/ the php script gets invoked and prints the full URI:

    /test/blub/blab/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP