When executing a script that includes the library, I want to find the library of the calling script from the library, in Perl I use env:, $0which gives me the path to the calling script, in PHP __FILE__, the current script is given, so in the library it gives me the path to the library , not the path of the calling script.
from perl documentation: $ 0 Contains the name of the file containing the Perl script to be executed.
I think this can be done with debug_backtrace (), but is there another better / shorter method?
EDIT: (added sample code)
file: index.php
<?php
require 'locallib.php';
echo 'in original script = '.__FILE__.'<br />';
?>
file: locallib.php
<?php
require "lib.php";
echo 'in library "'.__FILE__.'"<br />';
?>
file: lib.php
<?php
if( $_SERVER['SCRIPT_FILENAME'] != '/var/www/html/index.php')
{
echo "Not allowed";exit;
} else
{
echo 'in library "'.__FILE__.'"<br />';
echo '<pre>';
print_r($_SERVER);
echo '</pre>';
}
?>
source
share