Reputation: 3108
Is there any way to find out if a php script is accessed directly or it's included.
Upvotes: 11
Views: 1686
Reputation: 3065
Used ceejayoz's answer but I have troubles when using symlinked files and paths due to __FILE__
being resolved to the real path automatically. So I used realpath()
function on $_SERVER['SCRIPT_FILENAME']
instead to resolve both paths to the real path to solve the problem.
if(__FILE__ != realpath($_SERVER['SCRIPT_FILENAME'])) {
// we're in an include
}
Upvotes: 3
Reputation: 179994
if(__FILE__ != $_SERVER['SCRIPT_FILENAME']) {
// we're in an include
}
Upvotes: 16