24

I thought this would be simple but I can't seem to find a variable of $_SERVER array that has what I'm looking for.

Let's say my url is http://example.com/subdirectory/index.php I want to get all but the filename - http://example.com/subdirectory/.

I know I could quite easily do this with some string manipulation, but I want to know if there's a var of the _server array that I'm just missing. I've tried all of them to see what they give and I can get anything BUT what I'm looking for.

Vishnu R
  • 1,859
  • 3
  • 26
  • 45
MrVimes
  • 3,212
  • 10
  • 39
  • 57
  • possible duplicate of [Get the full URL in PHP](http://stackoverflow.com/questions/6768793/get-the-full-url-in-php) – T.Todua Apr 16 '15 at 13:36

7 Answers7

47

There won't be one because it's not a useful attribute to track. If the current script is something other than index.* then it will not return the correct URL for the current script.

However, this might get you what you want:

echo '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);

Edit:
Also, aside from installing the php.net online manual search tool for Firefox and setting it up with a search keyword (which will let you pull up the documentation on the $_SERVER global by typing something like "php $_server" into your URL bar), you should also know that var_dump() and print_r() lets you output the full contents of any variable, including arrays and objects. This is very useful for debugging.

Lèse majesté
  • 7,923
  • 2
  • 33
  • 44
  • 1
    But be aware, you should not assume that dirname() equals to the url path. – Jack Fuchs Dec 12 '12 at 08:33
  • In my case it doesn't work, get a url like `http://magento.p111111.webspaceconfig.de/magento.p111111.webspaceconfig.de/` – Stefan Brendle Dec 23 '13 at 08:31
  • @Stefan: You could try `$_SERVER['REQUEST_URI']` as JackFuchs wrote. Perhaps you're using some form of URL-rewriting that has docroot specified one level up from where your wwwroot is served from. Let me know if that works for you. – Lèse majesté Dec 23 '13 at 10:00
  • I get two slash before direname – Gino Dec 27 '15 at 21:29
  • Watch out that dirname doesn't end with a slash unless it's the home page, where it's just "/" so if you do dirname($_SERVER['PHP_SELF']) . "/folder"; you'll have 2 slashes in a row only if it's on the home page. – Curtis Jun 25 '18 at 23:01
18

In your case, string manipulation is the best solution.

For Example:

substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/') + 1);
Jack Fuchs
  • 400
  • 3
  • 7
9

There are no defined variables for what you want.

You can use the code below to get the full base url:

// Base folder url
$myBase =  ( (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')) ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

but hopefully most times you won't require the full thing just use:

// Base folder server root path
$myBase = dirname($_SERVER['PHP_SELF']);

Just be careful if you're using some kind of routes system (mod_rewrite).

Carlos Ouro
  • 565
  • 6
  • 16
3

I can't seem to find a variable of $_SERVER array that has what I'm looking for.

because it's fictional string, existing only in the browser's address bar.
It's parts being sent to server separated.

I want to know if there's a var of the _server array

That's very easy to know. just print_r($_SERVER); really simple.

I know I could quite easily do this with some string manipulation

yeah. the code you have to write is less than this question text.

but with some manual magic it can be reduced to just one function.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
2

you can use dirname() to get the path of a url.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
2

This worked perfectly for me and seems to be a very clean solution.

$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$path = $protocol.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
Dharman
  • 30,962
  • 25
  • 85
  • 135
user2288580
  • 2,210
  • 23
  • 16
2

Another solution, without dirname and respecting http and https is to use the old fashioned get path method (source from this stackoverflow answer). Then check if the path ends with the fileextension ".php". If true, remove it from the path.

Long version

$current_url_path = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

if (substr($current_url_path, -4) == ".php") {
    // path ends with .php
    // split string to array
    $arr = explode("/", $current_url_path);

    // remove last array item (after the last / which is the file)
    $arrS = array_slice($arr, 0, -1);

    // join the array to a string
    $current_url_path = implode("/", $arrS);
}
echo $current_url_path;

Shortened version

$current_url_path = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$current_url_path = substr($current_url_path, -4) == ".php" ?
    implode("/", array_slice((explode("/", $current_url_path)), 0, -1)) : $current_url_path;

echo $current_url_path;

Test

This url: http://localhost/comp-test/test.php is returned as http://localhost/comp-test

Advena
  • 1,664
  • 2
  • 24
  • 45