Php require()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Php require()

Post by Mince »

Hi all

I'd like to know if there's a method where you could see which page used the require() function to call another.

for example:

Code: Select all

<?php
require("newpage.php");
?>
Now on "newpage.php", i would like to know which page 'called' it.

Thanks
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Php require()

Post by Darhazer »

if this is run via web, try $_SERVER["SCRIPT_NAME"]; or $_SERVER['PHP_SELF']
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Re: Php require()

Post by Mince »

I found the answer on yahoo answers:

Code: Select all

$bt = debug_backtrace(); //get the backtrace array
$previouspage = $bt[0]['file']; //outputs the file the current function is in. The current function is require, and that function is in the caller file
this gives the absolute filepath

Here is the link: http://answers.yahoo.com/question/index ... 112AAC06B0
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Php require()

Post by Darhazer »

using backtrace is good for debugging purposes, but slow for production
why you need this?
Post Reply