1

I have a php file that could be included in different places. I need to have some rule on what to show depending on the including file.

Using $_SERVER['REQUEST_URI'] is returning the path of the page itself not the parent file that's including it.

Lamar
  • 1,761
  • 4
  • 24
  • 50
  • Have you tried [this](http://stackoverflow.com/a/139874/1252947)? – Pedro Pinheiro Sep 06 '15 at 06:44
  • You could let the included file expect a specific variable to be defined, that will be set/defined in each including context, before the actual inclusion. This would also add more semantic value, instead of relying on a specific file path for different behaviour. – Carsten Sep 06 '15 at 06:45
  • @Pedro Pinheiro, This won't do, because it gives the directory path. All pages involved share the same directory. – Lamar Sep 06 '15 at 06:46
  • What about [this](http://stackoverflow.com/a/6804550/1252947)? – Pedro Pinheiro Sep 06 '15 at 06:48
  • @Carsten, I was hoping to centralize the logic of switching specific functionality on and off inside the included page itself, rather that worrying about adding that logic in every place where I need to add the include. – Lamar Sep 06 '15 at 06:50
  • It sounds to me like an awkward design choice. As your often-used included code has to know what other files are using it (dependency in both directions)... But then again, the second comment from @PedroPinheiro should do the trick. – Carsten Sep 06 '15 at 06:54
  • Pedro's solutions is not for my problem. You might be right regarding not relying only on the path from your point of view, but I just think this would makes it easier to maintain the code knowing it's only in one place. But I guess your suggestion is the only way around. – Lamar Sep 06 '15 at 06:59

2 Answers2

3

Try:

<?php
echo $_SERVER['SCRIPT_FILENAME'];

The full list can be found on the PHP website.

Maltronic
  • 1,782
  • 10
  • 21
0

I use spl_autoload_register so files are loaded when I use an instance of the class. I put all my global functions as public static functions of classes. I only have one include in my pages:

require "autoload.php";

The name of the page is the name of the class with a .php extension. Here's the code for my autoload.php page:

<?php
function my_autoload($sClassName)
{
    // LOAD THE CLASS.
    for($loop=0;$loop < strlen($sClassName);$loop++)
    {
        $thischar = substr($sClassName,$loop,1);
        if (($thischar != "_") && ((!ctype_alnum($thischar)) || (ctype_digit($thischar))))
        {
            // INVALID CLASS NAME.
            echo 'Class ' . $sClassName . ' has invalid characters.';
            return;
        }
    }
    $filename = dirname(__FILE__) . '/' . strtolower($sClassName) . '.php'; 
    if (file_exists($filename))
    {
        require($filename);
    } else
    {
        echo 'Error: file ' . $filename . ' not found looking for class ' . $sClassName;
    }
    return;
}
spl_autoload_register("my_autoload");
?>
Russell Harkins
  • 404
  • 3
  • 4