doulan8330 2017-08-20 15:44
浏览 27
已采纳

使用其他目录中的php文件并包含当前目录中的文件

Suppose, we have three directories A, B and C along with another engine directory.

Now I want to post a search query via POST request to ../engine/search.php in the index file of A, B and C, but tell the file search.php to include the config.php file from the respective directory (with different parameters for the search). I.e. I cannot simply include ../[A,B,C]/config.php in engine/search.php since it depends on where the search is called from.

Is there any way to do this without having to keep a separate copy of search.php in each directory and thus including directly the config file from the respective directory? This should be possible, since similar multisite PHP setups exists (e.g. Wordpress MU).

Maybe something like __DIR__ that would give the path of where the php file gets called from rather than where it is located.

For security reasons, I do not want to pass the paths as part of the post request.

  • 写回答

1条回答 默认 最新

  • duanbarong4617 2017-08-20 16:02
    关注

    After some comments on the question clarifying what you're attempting...

    One thing you can do is to include a hidden field in your search forms indicating what "type" of search it is. Something as simple as:

    <input type="hidden" name="SearchType" value="SearchA" />
    

    Then in your server-side code you can determine which file to include based on that value. Something like this:

    switch ($_POST["SearchType"]) {
        case "SearchA":
            include("../A/config.php");
            break;
        case "SearchB":
            include("../B/config.php");
            break;
        // etc.
    }
    

    That way the config is determined dynamically by the form being posted, and not relying on any state tracked server-side about the form which was previously loaded.

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

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢