0

What I want is to be able to get the host and the sub-folder of a particular site. For example: get http://example:81/test/ from http://example:81/test/pagename.php.

It should also work with same url http://example:81/test/ and with multiple levels of sub-folders such as http://example:81/test/test2/somepage.php.

It should not get the structure of the file within a folder itself such as http://example:81/test/images/page.php should still be http://example:81/test/ and not http://example:81/test/images/.

I tried using

$_SERVER['HTTP_HOST'] // only provides example:81
$_SERVER['REQUEST_URI'] // provides full path example:81/test/images/

Essentially I want to get is the url of the index.php file even if it is in a sub-folder.

Is there any way to achieve this?

DominicM
  • 6,520
  • 13
  • 39
  • 60
  • You can try to do that with regular expression, can't think of anything simpler – Disenchanted May 01 '13 at 22:20
  • How can you make a difference between a subdomain and a simple sub-directory like `images` ? `parse_url` will allow you to extract all parts of the URL but it will not know if its a sub domain or a simple directory, these function work on string, that's all. – MatRt May 01 '13 at 22:24
  • @MatRt subdomain comes before the domain and a sub-directory comes after /. for example `test.example.com/foo/bar.php`, `test` is the subdomain part for host `test.example.com` and `/foo/` is the subdirectory. – Jonathan Kuhn May 01 '13 at 22:27
  • Unelss this is for your own site, it's a somewhat pointless exercises. URL paths do not have to correspond in ANY WAY to actual server-side file-system paths. e.g. `http://example.com/1/2/3` could very well actually be run by a script `root.php?x=1&y=2&z=3`, with no `/1/2/3` directories on the server at all. – Marc B May 01 '13 at 22:28
  • have you tried $_SERVER["SCRIPT_FILENAME"] ? – Ryan May 01 '13 at 22:29
  • @MarcB Reason I want this is so I could easily move the entire site(it's database-less) to a sub-folder for testing for example. – DominicM May 01 '13 at 22:30
  • @rncrtr I tried but what I get is this: /var/services/web/site/test/index.php – DominicM May 01 '13 at 22:33

2 Answers2

3

You would probably have to build your own string to get the full url.

<?php
$url = 'http'.(isset($_SERVER['HTTPS'])?'s':'').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

If you want details from that, you can use parse_url()

<?php
$details = parse_url($url);
print_r($details);

would output something like:

Array
(
    [scheme] => http
    [host] => example.com
    [port] => 81
    [path] => /test/test2/
    [query] => somepage.php
)

Edit:

If you want the path to the file, you can use $_SERVER['PHP_SCRIPT'] to get the filename of the called script relative to the document root or $_SERVER['SCRIPT_FILENAME'] to get the server path including the document root. There are also the built in constants __DIR__ and __FILE__ that have the server path up to the current script, even when included. All of these can be used with dirname() to get the directory of the variable and basename() to get just the filename. There is also $_SERVER['DOCUMENT_ROOT'] that has the path up to the web root.

So to get a path under the same directory this script resides in, if the script /var/www/test/index.php is called, you can use dirname(__FILE__).'/some/sub/dir/'.

You can see all the server variables by just doing a print_r of $_SERVER or even better you can call phpinfo() to get a nice output of all defined php modules and variables.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • I guess my question was too specific to answer fully(without all my code posted). Your answer was very helpful though. Is there a tydier way to express first code sample, or is it by far the "best" way as it is? Also, $_SERVER[HTTP_HOST] is returned with port already so if I use your code as it is I get duplicate port, any way around this, or do I even need to worry since maybe port is always included in the http_host? – DominicM May 02 '13 at 18:05
  • @DominicM That is the only way I know. And I don't have a server on a non-standard port so I didn't know the port was included with `HTTP_HOST`. You can remove the port part (I'll edit and remove it). If you don't need to switch between http/https you can remove the check for https also. You can see other examples here: http://stackoverflow.com/questions/6768793/php-get-the-full-url – Jonathan Kuhn May 02 '13 at 21:39
0

i had a similar problem in a project of mine; i wanted to be able to get the subfolder via a script filename (etc. index.php) I tried some methods but the following (ugly) method worked. The trick is to "feed" the method with all the possible "places" that all .php files can exist.

If the script name is found into one of these places, then subtract the part LEFT of those places; this part should be the subfolder (if there's any).

Yes, its ugly but worked for me...

$string = $_SERVER["SCRIPT_NAME"];
$subfolder = NULL;
// tries to find the 'subfolder' by checking all possible PHP calls
$places = array("/","plugins/","system/php");
foreach($places as $key=>$val) {
   $pos = strpos($string,$val);
   if ($pos !== FALSE && !isset($subfolder)) {
     $subfolder = substr($string,0,$pos-1);
     break;
   }
} 
  • I ended up simply subtracting the link names that I had in an array from the current url then appending it to the end if it's not currently selected link. – DominicM May 02 '13 at 18:08