How to detect if a file is being included or directly ran

前端 未结 6 984
春和景丽
春和景丽 2021-01-20 00:09

I have a php file that I include in my php script, but I don\'t want people to be able to directly run the file(without being included). How can I prevent that from happenin

6条回答
  •  感动是毒
    2021-01-20 01:01

    I use a sentinel constant, defined in the "including" script, and the "included" script looks for it.

    Example:

    /* main.php */
    define('SENTINEL', 1);
    include "included.inc.php"
    
    /* included.inc.php */
    if (!defined('SENTINEL')) die("Wanted to be smart?? Nice try.");
    

提交回复
热议问题