I have a log() method and want to log the name of the script that called it. How is that possible in PHP?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Depending on the calling environment, you should have a look at debug_backtrace, and $_SERVER[‘PHP_SELF’]
debug_backtrace()will give you a stack trace of the includes and function calls so far, and$_SERVER['PHP_SELF']will tell you the currently executing script, which is easier and might work just as well for what you want.$_SERVER['PHP_SELF']will pretty much always be the script that was called from the browser, for example, if you hadblah.com/admin.phpandblah.com/articles.phpthat both called/pages.phpto get a list of the pages saved in a blog or something, and something went wrong — the log would tell you whetheradmin.phporarticles.phpwas calling the script. But ifpages.phpincludedfunctions.phpwhich includedlibs.phpand libs.php failed, and you wanted to know thatfunctions.phpincluded it, this wouldn’t work — the log would still show the script that started the including (admin.phporarticles.php). In this case you would usedebug_backtrace().