Search code examples
phpphp-include

Detect If PHP is not opened by include


Is there, in any way, possible to detect if a PHP file is browsed by the browser itself and not by include?

I'm using PHP to process some code and then write it in another page in which this PHP is included (using the PHP include()). I don't want it to be browsed by itself and I want it to be accessed only if it is included (again, using the PHP include())

I wanted to achieve something like this:

if (browser is not opened by include){
    header("Location: /forbidden");
    die();
}
else {//do nothing}

Now, is it possible for me to achieve what I want?


Solution

  • Looks like you can try the reverse method of this answer:

    PHP CHeck if a File is Loaded Directly Instead of Including

    Instead of adding the define('APP_RAN') in the "base" file, add it to the include and add the other snippet to the base file.