4

While working on one of my PHP project I got a function debug_backtrace() at the start of code file as <?php debug_backtrace() || die ("Direct access not permitted"); ?> .

While studding on it I got some explanation that it works as:

Debugging PHP issues in a Drupal website can be anything from quick and easy to seriously problematic. PHP includes a debugging function called debug_backtrace , which will print out the chain of code that leads up to the point where the backtrace function is called.

And When I use var_dump() with debug_backtrace() I got following result:

array(2) {
  [0]=>
  array(3) {
    ["file"]=>
    string(61) "C:\xampp\htdocs\folder_name\templates\default\models\home.php"
    ["line"]=>
    int(30)
    ["function"]=>
    string(7) "include"
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(37) "C:\xampp\htdocs\folder_name\index.php"
    ["line"]=>
    int(146)
    ["args"]=>
    array(1) {
      [0]=>
      string(61) "C:\xampp\htdocs\folder_name\templates\default\models\home.php"
    }
    ["function"]=>
    string(7) "include"
  }
}

I didn't get that what debug_backtrace() function exactly work.

Please anyone explain is welcome. Thanks in advance.

Studied links:

debug_backtrace() from registered shutdown function in PHP

Debugging PHP Code with debug_backtrace

Assign debug_backtrace() To a variable in PHP

Print PHP Call Stack

How can I save a PHP backtrace to the error log?

jps
  • 20,041
  • 15
  • 75
  • 79
Ganesh Aher
  • 1,118
  • 3
  • 21
  • 51
  • You mean what it's doing in this case or in general? In this case if the script isn't called as part of an application, there is no application back trace and so it blocks you just running that particular script. – Nigel Ren Aug 23 '17 at 07:50
  • As you said that in this case it will block just running the script, that means it work as session ? I'm totally new for this function: debug_backtrace(), will you please explain its general working and how it works in my case. – Ganesh Aher Aug 23 '17 at 08:00

2 Answers2

7

Giving a basic example...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "Start...";
print_r(debug_backtrace());

function t1 ()  {
    echo "Start t1...";
    print_r(debug_backtrace());

}

function t2()   {
    echo "Start t2...";
    print_r(debug_backtrace());
    t1();
}

echo "before calls...";
print_r(debug_backtrace());
t1();
t2();

Will output...

Start...Array
(
)
before calls...Array
(
)
Start t1...Array
(
    [0] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 22
            [function] => t1
            [args] => Array
                (
                )

        )

)
Start t2...Array
(
    [0] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 23
            [function] => t2
            [args] => Array
                (
                )

        )

)
Start t1...Array
(
    [0] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 17
            [function] => t1
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 23
            [function] => t2
            [args] => Array
                (
                )

        )

)

I hope this shows that all the debug_backtrace function does is return what has been called so far. So when you first call t1, it is simply a stack trace of the call to t1. The same for the start of t2, but when t2 calls t1, the trace lists the call to t2 and t1.

But also as you can see, that debug_backtrace from Start.. shows nothing, as there are no levels of code that have caused this trace to be printed.

In your case it calls debug_backtrace and uses the or die() to say 'if debug_backtrace returns nothing, then die()'

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • [This article](https://www.scalyr.com/blog/php-stack-trace-understanding-debug/) might be useful for users who don't know about [stack trace](https://en.wikipedia.org/wiki/Stack_trace). – user31782 Jan 26 '20 at 13:43
1

I think the most useful method for debug_backtrace() is to trace which file is the init one start the core, example like

a.php
include 'b.php';

b.php
function test(){
     echo('whatever');
}
print_r(debug_backtrace())

php a.php


whateverArray
(
    [0] => Array
        (
            [file] => /opt/php/b.php
            [line] => 7
            [function] => test
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /opt/php/a.php
            [line] => 3
            [args] => Array
                (
                    [0] => /opt/php/b.php
                )

            [function] => include
        )

)

So, you can get from

$backtrace = debug_backtrace()
echo $backtrace[count($backtrace) - 1]['file'];

Get the file who start the calling