Posted by & filed under PHP, Programming.

Sometimes it is useful to know if a page has been included by another page or not, and then take action based on the result. A quick and handy way to accomplish this is to use a tenary operator to do something like:

$included = strtolower(realpath(__FILE__)) != strtolower(realpath($_SERVER['SCRIPT_FILENAME']));

Which will return true if it is included, and false if it is running as itself. Now, we are able to do something like:

if($included == false)
    echo 'derp!';

Leave a Reply

You must be logged in to post a comment.