21

Here is an example of what I am trying to do:

index.php

<ul><?php include("list.php") ?></ul>

list.php

<?php
    if (PAGE_NAME is index.php) {
        //Do something
    }
    else {
        //Do something
    }
?>

How can I get the name of the file that is including the list.php script (PAGE_NAME)? I have tried basename(__FILE__), but that gives me list.php.

2
  • Why do you need to do this? There may be a workaround for your situation.
    – esqew
    Jul 24, 2011 at 2:10
  • I have two types of files, index.php, and the rest of the files are articles. The articles are in articles/year/month/day/name.php each use list.php to get a list of articles which are displayed in a bar, the problem is the location of the script that connects to my database further from the article pages. So I am doing the conditional to see if I need to include(../inc/conn.php) or include(../../../../../inc/conn.php)
    – valon
    Jul 24, 2011 at 2:16

6 Answers 6

28

$_SERVER["PHP_SELF"]; returns what you want

0
7

If you really need to know what file the current one has been included from - this is the solution:

$trace = debug_backtrace();

$from_index = false;
if (isset($trace[0])) {
    $file = basename($trace[0]['file']);

    if ($file == 'index.php') {
        $from_index = true;
    }
}

if ($from_index) {
    // Do something
} else {
    // Do something else
}
2
  • 2
    Or, just the file, debug_backtrace()[0]['file'] Jun 6, 2017 at 18:46
  • Brilliant shorthand, @SolomonUcko!
    – Rounin
    Oct 23, 2019 at 14:39
7

In case someone got here from search engine, the accepted answer will work only if the script is in server root directory, as PHP_SELF is filename with path relative to the server root. So the universal solution is

basename($_SERVER['PHP_SELF'])

Also keep in mind, that this returns the top script, for example if you have a script and include a file, and then in included file include another file and try this, you will get the name of the first script, not the second.

1
  • Thanks for explaining behavior in the case of nested includes! Sep 22, 2016 at 15:00
2

In the code including list.php, before you include, you can set a variable called $this_page and then list.php can see the test for the value of $this_page and act accordingly.

2

Perhaps you can do something like the following:

<ul>
    <?php
        $page_name = 'index';
        include("list.php")
    ?>
</ul>

list.php

<?php
    if ($pagename == 'index') {
        //Do something
    }
    else {
        //Do something
    }
?>
1
  • Doesn't work. Firstly you made a mistake with $page_name and $pagename. Then list.php won't be able to see $page_name assuming you used $page_name in both scripts
    – user5294439
    Jun 18, 2016 at 8:12
1

The solution basename($_SERVER['PHP_SELF']) works but I recommend to put a strtolower(basename($_SERVER['PHP_SELF'])) to check 'Index.php' or 'index.php' mistakes.

But if you want an alternative you can do:
<?php if (strtolower(basename($_SERVER['SCRIPT_FILENAME'], '.php')) === 'index'): ?>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.