-1

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.

random guy
  • 201
  • 3
  • 9
  • Why does including the config depend on where the search is called from? I guess I don't really understand what you're trying to do here or why you can't just specify the PHP file you want to include. – David Aug 20 '17 at 15:47
  • search.php needs to read from config.php. Now I want to reuse this search.php in another directory which has different parameters in config.php – random guy Aug 20 '17 at 15:48
  • 1
    You could include a hidden field in the search form which indicates what type of search it is. Then the server-side code could use that field as a flag to determine which file to include. – David Aug 20 '17 at 15:49
  • I was thinking about that, but it is not so nice, since I´d have to change search.php each time I want to reuse it from another directory. Anyway, I'm upvoting your suggestion :) – random guy Aug 20 '17 at 15:51
  • Why would you need to change it? It can just have a simple `if` statement or `switch` statement of some kind to determine which dependent file to include. That's your goal, isn't it? To conditionally include a different file depending on some indicator? So why can't a hidden field be that indicator? – David Aug 20 '17 at 15:52
  • So you're suggesting to pass just the relative path part as some kind of a flag? – random guy Aug 20 '17 at 15:55
  • Well, not the actual path of course. Allowing users to control what files you load would be a security risk. But an indicator of some sort, which your server-side code would check and then use the known path for that indicator. Something as simple as a name for the search would do the trick. – David Aug 20 '17 at 15:56
  • So I would still have to update the name to path mapping each time. I was hoping there is some other less hacky / more cannonical way to do it. – random guy Aug 20 '17 at 15:58
  • Yes, you'd have to write a few lines of code to determine which file to include. If you're trying to do this *without writing any code* then that's not going to work very well. – David Aug 20 '17 at 15:59
  • Are you familiar with router libraries? Like this one https://github.com/nikic/FastRoute ? They might help you in this situation. You necessarily do not need to put your files in actual directories `A`, `B` and `C`. If you're good with @David solution, then you might want too write some code to use some sort of directory mapping, and only update that mapping each time. – Nima Aug 20 '17 at 16:01
  • Not really, but researching on this brought me to https://stackoverflow.com/questions/10265216/get-calling-file-name-from-include which is exactly what I wanted to achieve. – random guy Aug 20 '17 at 16:10

1 Answers1

0

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.

David
  • 208,112
  • 36
  • 198
  • 279