Methods for prohibiting illegal calls and the introduction of hard path files in PHP
There are some common files in PHP for convenience, we will make a common file, so that the unused files are called together. In order to prohibit public files from being called very separately,
You can make a constant on the file, prohibiting very calls:
Define a constant on the public file as follows
Define (' custom_string ', true);
In the file to be called, add a judgment to call the public file:
if (!defined (' custom_string ')) { exit (' illegal call '); }
In general, to invoke the file can be used Include,include_once,require, require_once to call, but from a speed perspective, you can use the hard path to introduce the file.
In PHP with the magic constant __file__ can get the path of the file, with DirName (__file__), you can get the root directory address,
If you introduce a header.php file, you can introduce it in the following ways:
Require DirName (__file__). ' /filename.php ';
However, sometimes it may be inconvenient to call this Dirnam, which can be converted into a constant to make it easier to invoke.
The substr () function in PHP can intercept the root directory of a file. You can define this path as a constant:
Define (' Site_path ', substr (DirName (__file__), 0,-n));
A negative number allows the file to be intercepted from the back, and N is the amount of path characters stored in the public file. For example, if the public file store relative path is abc/includes/filename.php, then the number of characters of N abc/includes: 12. You can use constants to introduce a path directly after you define it as a constant:
Require Site_path. ' Filename.php ';
Methods for prohibiting illegal calls and the introduction of hard path files in PHP