doulan8330 2017-08-20 07: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 08: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部