PHP: Equivalent of include using eval

后端 未结 8 1265
清酒与你
清酒与你 2020-12-06 05:56

If the code is the same, there appears to be a difference between:

include \'external.php\';

and

eval(\'?>\' . file_get_conten

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-06 06:13

    As noted by @bwoebi in this answer to my question, the eval substitution does not respect the file path context of the included file. As a test case:

    Baz.php:

    Foo.php:

    ' . file_get_contents('Baz.php',  FILE_USE_INCLUDE_PATH)) . "\n";
    echo (include 'Baz.php') . "\n";
    

    Result of executing php Foo.php:

    $ php Foo.php 
    /path/to/file/Foo.php(2) : eval()'d code
    /path/to/file/Baz.php
    

    I don't know of any way to change the __FILE__ constant and friends at runtime, so I do not think there is any general way to define include in terms of eval.

提交回复
热议问题