Forum Moderators: coopster

Message Too Old, No Replies

Getting current directory name

another newbie question

         

Reflection

7:50 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



What I am trying to do is have a file(which writes a section of html) that I am including in my pages but in certain sections of my site I want to execute different code(ie send different html).

Example...

www.example.com/dirA/pageA.php
www.example.com/dirB/pageB.php
www.example.com/includes/includedfile.php

On both pages I would like to include the same file, and inside that file I would like to determine which directory the 'calling' page is located in. I would then use a switch statement to execute the appropriate code.

Something like...

switch ($directoryName)
{
case "dirA":
....
break;
case "dirB":
...
break;
...
}

Is this possible or am I going about this entirely the wrong way :)?

Thanks.

dmorison

7:58 pm on Dec 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can inspect $_SERVER["REQUEST_URI"] to determine the page that has called the include but there may be an easier way.

What I would do is insert a phpinfo() [uk.php.net] in your included file; have a look at the global variables available to your script and see which would be the easiest one to use in your situation.

Reflection

8:10 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



Thanks dmorison, it looks like REQUEST_URI is the best option, I will just have to figure out how to parse the string for the directory.

Thanks

dmorison

8:18 pm on Dec 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're always looking at the first level directory; do an explode("/",$REQUEST_URI) and look at array element 0.

Reflection

8:37 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



Well that was easier than I thought...

$strArr = explode("/", $_SERVER["REQUEST_URI"]);
$directory = $strArr[sizeof($strArr) - 2];
print($directory);

That accomplishes what I need.

Thanks for your help, much appreciated.