random guy random guy - 1 month ago 18
PHP Question

Use php file from other directory and include file from current directory

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.

Answer Source

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.