Prevent direct access to a php include file

后端 未结 30 1018
盖世英雄少女心
盖世英雄少女心 2020-11-22 06:32

I have a php file which I will be using as exclusively as an include. Therefore I would like to throw an error instead of executing it when it\'s accessed directly by typing

30条回答
  •  遇见更好的自我
    2020-11-22 06:59

    If more precisely, you should use this condition:

    if (array_search(__FILE__, get_included_files()) === 0) {
        echo 'direct access';
    }
    else {
        echo 'included';
    }
    

    get_included_files() returns indexed array containing names of all included files (if file is beign executed then it was included and its name is in the array). So, when the file is directly accessed, its name is the first in the array, all other files in the array were included.

提交回复
热议问题