1

I have a PHP script that will allow me to track which file required it via debug_backtrace(). I wanted to auto prepend (Via auto_prepend_file within php.ini) the file so then I could build a log of the frequency in which each PHP script is ran for internal analysis.

It works fine when I create a file that requires it however it causes an error when I "auto_prepend_file" it. My understanding of auto prepend was that it will require whatever file you identify. However, the returned array of debug_backtrace appears to be empty when auto-prepending. Any ideas on what may be happening?

Code example for those curious

first.php

<?PHP
require('second.php');
?>

second.php

<?php 
var_dump(debug_backtrace());
?>

For the actual auto prepending I'm just identifying second.php. When auto prepending I get:

array(0) { }

When going to first.php within the browser I get:

array(1) { [0]=> array(3) { ["file"]=> string(39) "/var/www/first.php" ["line"]=> int(4) ["function"]=> string(7) "require" } }

Any and all help is appreciated!

Thanks.

Ron
  • 181
  • 3
  • 12
  • Isn't this something you could use xdebug for? Anyway, show some code – Michael Feb 10 '16 at 04:59
  • Code example added. I've never used xdebug before, I'll check it out. – Ron Feb 10 '16 at 05:10
  • Where is the "auto prepending code"? — please show your logic, for clarity – Michael Feb 10 '16 at 05:12
  • I'm specifying "/var/www/second.php" @ auto_prepend_file within php.ini. Sorry I should have been more specific. (I added a clarifying statement) – Ron Feb 10 '16 at 05:13
  • What is your aim of all this? It sounds like you are trying to brew your own autoloading scheme. Don't do that. Use composer or at least the built-in functionality. For statistics I'm sure you can make use of xdebug or the likes – Michael Feb 10 '16 at 05:16
  • So what will you do with the backtrace? – Michael Feb 10 '16 at 05:17
  • I inherited a pretty large project that contains a lot of legacy php code. I just wanted a simple way to track which scripts were actually being used over the course of a month or so in order to do some cleaning. I initially was going through the access.log but it was spread over a bunch of files and was getting difficult to parse. – Ron Feb 10 '16 at 05:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103043/discussion-between-michael-and-ron). – Michael Feb 10 '16 at 05:20

0 Answers0