Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out a php file is included or accessed directly?

Tags:

php

Is there any way to find out if a php script is accessed directly or it's included.

  1. Suppose we don't where it's included, so setting session variable won't solve the problem. I can not check where it's included. it may be more than 20 places!
  2. It should be detected in php script. (via php functions and variables)
like image 371
hpaknia Avatar asked Apr 22 '13 18:04

hpaknia


People also ask

How do you check if file is already included PHP?

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.

How files can be accessed in PHP explain with an example?

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.

What does __ FILE __ mean in PHP?

__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.

What are include () and require () functions?

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.


1 Answers

if(__FILE__ != $_SERVER['SCRIPT_FILENAME']) {
  // we're in an include
}
like image 184
ceejayoz Avatar answered Oct 06 '22 01:10

ceejayoz