I was wondering if it's possible to detect programmatically whether a script has been included/required inside another script or whether it is executing standalone.

For example, if I have a script called process_data.php, can I add code to process_data.php that will tell me whether I called it directly:

http://host/process_data.php
/usr/local/bin/php -q process_data.php

or whether I called it from another file:

include('process_data.php');
require('process_data.php');

Basically, I'm trying to write some functions into process_data.php that can be imported into other code but, if the file is called directly, I want a different set of functions (a main() block to borrow from C) to execute instead.

I know I can do this by checking for the existance of a variable (say $script_included = TRUE ) but this requires setting the variable in all the code that is using this file. I'd rather find a way for the process_data.php script to do it autonomously.

Thanks

    You can test if functions exist with the function_exists() function. But for what you're asking for, it really depends on what code you have involved.

    You could also look into using the $SERVER['argc'] and $SERVER['argv'] variables to determine if the script has been run from the command line or not. I believe $_SERVER['argv'][0] will be the command that has execute the script. If it was run from the web server I don't believe this variable would be available (although I haven't checked).

    Personally, I'd look at how you have process_data.php setup. I'd split it into 2 files. One that contains all its functions. Then the 2nd one containing the code to execute those functions - handy for when you wish to execute the file on the command line. Then for your include()/require() situations, you could just include the file that contains the functions.

      Yeah, that makes a lot of sense to split my "main()" and the functions off from each other into separate files. But I'll have to try the argv/argc parameters just in case I want to do something like this later. I didn't know argv/argc were even in there and that will come in handy for some existing scripts that need a better system.

      Thanks

        8 years later

        I was looking for a way to determine if a file have been included or called directly, all from within the file. At some point in my quest I passed through this thread. Checking various other threads and pages from the PHP manual I got enlightened and came up with this piece of code:

            if ( basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"]) ) {
              echo "called directly";
            }
        
        else {
          echo "included/required"
        }
        

        In essence it compares if the name of the current file (the one that could be included) is the same as the file that is beeing executed.


        EXPLANATION:

        • FILE is a PHP magic constant that stores the full path and filename of the file, the beauty of it is that if the file has been included or required it still returns the full path and filename of such file (the included file).<br>
          (Magic Constants Manual: http://php.net/manual/en/language.constants.predefined.php)

        • $SERVER["SCRIPT_FILENAME"] returns the absolute pathname of the currently executing script. As when a file is included/required it's not executed (just included) it returns the path name of the (let's say) "parent" file (the one that includs the other file and the one that gets executed).<br>
          ($
          SERVER: http://php.net/manual/en/reserved.variables.server.php)

        • basename(string $path) is a function that returns the trailing name component of path, that in this case is the file name. You could also just compare the full path and filename, that would be indeed better, it isn't really neceseary to use this function but it feels cleaner this way, jajaj.<br>
          (basename(): http://php.net/manual/en/function.basename.php)

        Actualy I borrowed this user (from bugmenot.com) just for the spirit of being helpful, It wasn't worthy to create an account. I hope this to be useful to you and others also passing by.


        EN ESPAÑOL

        Como determinar si un archivo a sido incluido o llamado directamente. Si no se entiende la respuesta o no manejan inglés mándenme un mail que no tengo problema en responder, no vi la necesidad de pasar todo a españor pero por las dudas (interwebs_cowboy@yahoo.com.ar). Saludos.


        IN ITALIANO

        Come determinare se un file è stato incluso (include/require) o chiamati direttamente. Non parlo molto italiano ma posso aiutare capire la risposta se mi scrivi un mail (interwebs_cowboy@yahoo.com.ar). Ciao.

          Write a Reply...