Jump to content

Finding the location a script is running in?


jrcarr

Recommended Posts

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:
[code]
<?
echo $_SERVER['DOCUMENT_ROOT']. "<br>";
echo $_SERVER['SCRIPT_FILENAME']. "<br>";
echo $_SERVER['SERVER_NAME']. "<br>";
echo $_SERVER['SCRIPT_NAME'];
[/code]

[b]I get the following information:[/b]
/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
Link to comment
Share on other sites

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

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
Link to comment
Share on other sites

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:

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

Using the first example with a "test" directory, it should give me:
[code]http://www.domain.com/test[/code]
But if sitting in the root directory, it wouldn't work, since it would read like:
[code]http://www.domain.com/public_html[/code]
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.