0

I have a parent function that is passed a variable called $scriptName. Depending on what is stored in $scriptName, I want to call the corresponding script.

I have a file called childOneScript.php

If $scriptName=="childOne", how do I call childOneScript.php?

2
  • How does childOneScript.php expect to be called? By file inclusion, or does it expect to be requested by the browser? Commented Feb 4, 2015 at 15:41
  • require 'childOneScript.php'. Commented Feb 4, 2015 at 15:41

4 Answers 4

2

You can just use the normal require

require_once $scriptName . 'Script.php';

Keep in mind however that if the script does not exist PHP will raise a fatal error, so you should be checking that the script does indeed exist.

/**
   Assumes that $name does not contain the PHP extension and
   this function works with relative paths.
   If the file does not exist, returns false, true otherwise
*/
function loadScript($name) {
    $name = $name . '.php';
    if (!file_exists($name) || !is_readable($name)) {
        // raise an error, throw an exception, return false, it's up to you
        return false;
    }
    require_once $name;
    return true;
}
$loaded = loadScript('childOneScript');

Alternatively you can use include, PHP will only raise a warning if it can't find the script.

There are a few security concerns with the above function. For example if the user is allowed to define the value of $scriptName an attacker could use it to read any file that is readable to the web server user.

Here is an alternative that limits the number of files that can be dynamically loaded to just the files that need be loaded in this manner.

class ScriptLoader {

    private static $whiteList = array(
        // these files must exist and be readable and should only include
        // the files that need to be loaded dynamically by your application
        '/path/to/script1.php' => 1,
        '/path/to/script2.php' => 1,
    );

    private function __construct() {}

    public static function load($name) {
        $name = $name . '.php';
        if (isset(self::$whiteList[$name])) {
            require_once $name;
            return true;
        }
        // the file is not allowed to be loaded dynamically
        return false;
    }
}

// You can call the static method like so.
ScriptLoader::load('/path/to/script1');     // returns true
ScriptLoader::load('/path/to/script2');     // returns true 
ScriptLoader::load('/some/other/phpfile');  // returns false
Sign up to request clarification or add additional context in comments.

3 Comments

@MrPk sorry I rejected your edit and then saw why you made it. apologies.
I accepted this one because I need it to be dynamic. The other answers would work though, but in my case, I need the dynamic solution.
Be very aware of security problems. If you let users control the value of $name this could easily turn into leaks
2

You can simply do:

if ($scriptName=="childOne"){
    require_once(childOneScript.php);
}

The require_once statement will check if the file has already been included, and if so, not include (require) it again.

Readup: require_once() | PHP

Comments

1

Just use the include statement inside the If condition.

if $scriptName == "childOne" {
     include childOneScript.php;
   }

Comments

1

You could use the include or require methods in PHP

<?php
function loadScript($scriptName) {
  if ($scriptName=="childOne") {
    include("childOneScript.php");
  }
}
?>

Keep in mind though that the included script is included where you load it. So it's inside the loadScript function. That means you cannot access it's content outside its scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.