Jump to content


Photo

Finding the location a script is running in?


  • Please log in to reply
3 replies to this topic

#1 jrcarr

jrcarr
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationBeautiful Arkansas Ozarks

Posted 29 October 2006 - 06:20 PM

I'm trying to figure out how to get the location that a file is executed.  For example, if I run a script in www.domain.com/test and have the following code:
<?
echo $_SERVER['DOCUMENT_ROOT']. "<br>";
echo $_SERVER['SCRIPT_FILENAME']. "<br>";
echo $_SERVER['SERVER_NAME']. "<br>";
echo $_SERVER['SCRIPT_NAME'];

I get the following information:
/home/canter??/public_html
/home/canter??/public_html/test/phpinfo.php
www.domain.com
/test/phpinfo.php

Now the info I need is, which is the actual location of the file being run:
/home/canter??/public_html/test  ($_SERVER['SCRIPT_FILENAME'] minus the file name and the ending slash)
http://www.domain.com/test    ($_SERVER['SERVER_NAME']+$_SERVER['SCRIPT_NAME'] minus the file name and the ending slash)

I know this can't be that difficult, but I'm am drawing a total blank.  Thanks

Jack
Jack Carr

#2 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 29 October 2006 - 07:07 PM

$scriptLocation = basename(dirname($_SERVER['SCRIPT_FILENAME']));

I believe would work, not sure if there's another way that's "easier".  I'll explain:

dirname(file) will retrieve the name of the directory the file is in but will be in full ie: /home/sites/name/public_html/
basename will retrieve the 'base' of either a filename or directory ie: basename(index.php) returns index
basename(/home/sites/name/public_html) returns public_html

Hope this helps,

Dest

#3 jrcarr

jrcarr
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationBeautiful Arkansas Ozarks

Posted 29 October 2006 - 08:12 PM

Ok, how about taking the info from $_SERVER['SERVER_NAME'], which in the example given would be "/home/canter??/public_html/test/phpinfo.php" and find and remove everything from the last accurance of the "/" to the end, leaving only "/home/canter??/public_html/test".  If the script/file is run at: "/home/canter??/public_html/phpinfo.php" it would leave "/home/canter??/public_html"
I'm just not sure what php command would look through a string and find the "last" accurance of a character and then replace everything from it to the end with "".

Then with server name, I could do something like the following, using the "basename" like you mentioned:

$scriptLocation = "http://" . ($_SERVER['SCRIPT_FILENAME']). "/" .basename(dirname($_SERVER['SCRIPT_FILENAME']));

Using the first example with a "test" directory, it should give me:
http://www.domain.com/test
But if sitting in the root directory, it wouldn't work, since it would read like:
http://www.domain.com/public_html
I can check for whether basename(dirname($_SERVER['SCRIPT_FILENAME'])) = "public_html"; and not include it, since not all servers are setup the same and it won't always be "public_html".

Does any of this make sense and is it as difficult as I'm trying to make it?

Jack
Jack Carr

#4 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 29 October 2006 - 08:27 PM

Okay, if you're using it in a sense that's used for external links you may wish to use something with an external location and not an absolute 'internal' path.  Perhaps using REQUEST_URI instead of SCRIPT_FILENAME.

HTH

Dest




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users