DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Is there a utility out there that will show me all includes

By: | 24 Jul 2009 11:50 am

I am working at a new job debugging lots of php that has been written by coders who are LONG gone and I`m wondering if there is such an animal available that will show me all of the includes that make up the output of a php script; i.e. when I go to a web page that is made of php, it`ll show me what files make up that page.

Otherwise I feel like a detective...and it takes so long to decode (I guess that`s why they call it code)

Thanks in advance.

Comments

A quick hunt around PHP.net and you`ll find get_included_files

http://php.net/get-included-files

By: | 24 Jul 2009

Real Programmers don`t comment their code. If it was hard to write, it should be hard to understand.

However, your quest is appropriate and when working with Drupal I would sometimes like to know which files were accessed for a given spider-friendly URL.

For one thing, it depends on what those includes contain. If they have just straight code which is run at the inclusion point, you may have to do something tricky to figure it out. However, if the includes (or requires) contain function definitions, you might be able to do something. You`d want to know which include files contain each function. However, to find out how many functions are available to the running script, you can use:

http://php.net/get_defined_functions

and look at the "user" part of the array. For example:

$fn = get_defined_functions();
print_r($fn[`user`]);

Other useful functions for this sort of project would be:

http://php.net/function_exists
http://php.net/get_defined_vars
http://php.net/get_defined_constants
http://php.net/get_declared_classes

You could use the auto_prepend_file and auto_append_file in php.ini
(http://us3.php.net/manual/en/ini.list.php) to write bits of code at the
beginning and end which note any changes to the available functions, classes, variables, etc.

This still does not get you to knowing which files were included or required in your running page, however.

It might be possible to replace each instance of require(), include(),
require_once(), include_once() with your own custom functions that will register the file names in a session variable or database table. I`m not thrilled with this idea but it will depend on how deep you want to go into this.

On a Linux system I`d probably write a script (perhaps in bash) to scan through the PHP files and look for include or require functions to try to trace them.  Of course, such a script probably would not be smart enough to know if one of these calls was inside a conditional or loop structure so they might not apply to the runtime program.

I don`t know if PHP has any internal accounting of included files. If someone else knows of one, it would be interesting to learn about.

By: | 24 Jul 2009

No worries. It`s also worth looking at http://php.net/debug_backtrace and http://php.net/debug_print_backtrace which can be used to provide a trace from the current point in code out to the top of the call stack - so if you`re 5 functions calls deep it will let you know what each function call was and where (file + line) it was called.

By: | 24 Jul 2009

I was going to suggest to write a small script which would add something like this
<?PHP echo `Included filename <br />`; ?>

By: | 24 Jul 2009

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move