Is there any way to find out if a php script is accessed directly or it's included.
There are two easy approaches when you want to check if a file has been included, one is to use the get_included_files function, which is the recommended approach, and another is to use the include_once statement — but the latter does not work well, since it triggers a fetal error if a file has already been included.
PHP Read File - fread() The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.
__FILE__ is simply the name of the current file. realpath(dirname(__FILE__)) gets the name of the directory that the file is in -- in essence, the directory that the app is installed in.
The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.
if(__FILE__ != $_SERVER['SCRIPT_FILENAME']) {
// we're in an include
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With