Thursday 16 January 2020

Running a php file from another php file as if I opened it directly from the browser (relative path issue)

Suppose I have a PHP file that does important things.

doAllTheThings.php:

<?php
    include '../importantThing1.php';
    include '../importantThing2.php';
    include '../importantThing3.php';
    //lots more complicated code below

Let's also suppose I type the address into the web browser, it takes half a minute but all the things get done, known good, fantastic, let's not mess with it. (There's lots of valid reasons to not mess with it. Maybe I didn't write it and don't understand it. Maybe I'm not given access to it. Maybe this is an emergency patch, and don't have time to update and test every single include path.)

Now suppose I want the user to be able to do something that can cause the code to run.

customerInterface.php:

<?php
    //lots more complicated code above
    if(doTheThings){
        include '../../things/important/doAllTheThings.php';
    }

It runs, but now the relative paths don't work. The importantThing links are broken. No important things get done.

How do I execute "doAllTheThings.php" such that it will behave the same as if I typed its address directly into the browser address bar? (Without changing the directory structure, the file location, or "doAllTheThings.php")



from Running a php file from another php file as if I opened it directly from the browser (relative path issue)

No comments:

Post a Comment