douyan7916 2015-03-08 13:51
浏览 23
已采纳

PHP:如何检查函数中是否包含给定文件()

I have a PHP file that can be include'd() in various places inside another page. I want to know whether it has been included inside a function. How can I do this? Thanks.

  • 写回答

3条回答 默认 最新

  • du155305 2015-03-08 14:36
    关注

    There's a function called debug_backtrace() that will return the current call stack as an array. It feels like a somewhat ugly solution but it'll probably work for most cases:

    $allowedFunctions = array('include', 'include_once', 'require', 'require_once');
    foreach (debug_backtrace() as $call) {
        // ignore calls to include/require
        if (isset($call['function']) && !in_array($call['function'], $allowedFunctions)) {
            echo 'File has not been included in the top scope.';
            exit;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU