DGM
DGM

Reputation: 26979

How can I find an application's base url?

I'd like to find the base url of my application, so I can automatically reference other files in my application tree...

So given a file config.php in the base of my application, if a file in a subdirectory includes it, knows what to prefix a url with.

application/config.php
application/admin/something.php
application/css/style.css

So given that http://www.example.com/application/admin/something.php is accessed, I want it to be able to know that the css file is in $approot/css/style.css. In this case, $approot is "/application" but I'd like it to know if the application is installed elsewhere.

I'm not sure if it's possible, many applications (phpMyAdmin, Squirrelmail I think) have to set a config variable to begin with. It would be more user friendly if it just knew.

Upvotes: 10

Views: 23131

Answers (11)

Richard Leishman
Richard Leishman

Reputation: 9

This is what I have used in my app which works fine, I also use a custom port so I had to allow for this also.

define('DOC_URL', ($_SERVER['HTTPS']=='on'?'https':'http').'://'.$_SERVER['SERVER_NAME'].(!in_array($_SERVER['SERVER_PORT'], array(80,443))?':'.$_SERVER['SERVER_PORT']:'')).dirname($_SERVER['REQUEST_URI']);

Then I use it by typing the following...

<img src="<?php echo DOC_URL; ?>/_img/logo.png" />

Upvotes: 0

Murtaza Baig
Murtaza Baig

Reputation: 211

You can find the base url with the folowing code:

define('SITE_BASE_PATH','http://'.preg_replace('/[^a-zA-Z0-9]/i','',$_SERVER['HTTP_HOST']).'/'.str_replace('\\','/',substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT']))).'/');

Short and best.

Upvotes: 5

rakesh sadaka
rakesh sadaka

Reputation: 1

url root of the application can be found by

$protocol = (strstr('https',$_SERVER['SERVER_PROTOCOL']) === false)?'http':'https';
$url = $protocol.'://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['REQUEST_URI']);

this code will return url path of any application whether be online or on offline server.

I've not made proper check for proper '/'. Please modify it for slashes.

this file should be placed in root.

example : if application url :

http://localhost/test/test/test.php

then this code will return

http://localhost/test/test

Thanks

Upvotes: 1

Andrew Moore
Andrew Moore

Reputation: 95364

I use the following in a homebrew framework... Put this in a file in the root folder of your application and simply include it.

define('ABSPATH', str_replace('\\', '/', dirname(__FILE__)) . '/');

$tempPath1 = explode('/', str_replace('\\', '/', dirname($_SERVER['SCRIPT_FILENAME'])));
$tempPath2 = explode('/', substr(ABSPATH, 0, -1));
$tempPath3 = explode('/', str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])));

for ($i = count($tempPath2); $i < count($tempPath1); $i++)
    array_pop ($tempPath3);

$urladdr = $_SERVER['HTTP_HOST'] . implode('/', $tempPath3);

if ($urladdr{strlen($urladdr) - 1}== '/')
    define('URLADDR', 'http://' . $urladdr);
else
    define('URLADDR', 'http://' . $urladdr . '/');

unset($tempPath1, $tempPath2, $tempPath3, $urladdr);

The above code defines two constants. ABSPATH contains the absolute path to the root of the application (local file system) while URLADDR contains the fully qualified URL of the application. It does work in mod_rewrite situations.

Upvotes: 16

SchizoDuckie
SchizoDuckie

Reputation: 9401

This works like a charm for me, anywhere I deploy, with or without rewrite rules:

$baseDir = 'http://'.$_SERVER['HTTP_HOST'].(dirname($_SERVER['SCRIPT_NAME']) != '/' ? dirname($_SERVER["SCRIPT_NAME"]).'/' : '/');

Upvotes: 0

da5id
da5id

Reputation: 9136

I've never found a way to make it so.

I always end up setting a config variable with the server path to one level above web root. Then it's:

  • $configVar . 'public/whatever/' for stuff inside root,
  • but you can also include from outside with $configVar . 'phpInc/db.inc.php/' etc.

Upvotes: 0

UnkwnTech
UnkwnTech

Reputation: 90871

If you want the path on the filesystem you can use $_SERVER['DOCUMENT_ROOT']
If you just want the path of the file that appears in the URL after the domain use $_SERVER['REQUEST_URI']

Upvotes: 0

Adam Pierce
Adam Pierce

Reputation: 34365

One solution would be to use relative paths for everything, that way it does not matter where the app is installed. For example, to get to your style sheet, use this:

../css/style.css

Upvotes: 0

Lucas Oman
Lucas Oman

Reputation: 15872

Put this in your config.php (which is in your app's root dir):

$approot = substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT']));

I think that'll do it.

Upvotes: 1

Jay
Jay

Reputation: 42642

The REQUEST_URI combined with dirname() can tell you your current directory, relevant to the URL path:

<?php
  echo  dirname($_SERVER["REQUEST_URI"]);
?>

So http://example.com/test/test.php prints "/test" or http://example.com/ prints "/" which you can use for generating links to refer to other pages relative to the current path.

EDIT: just realized on re-reading that you might be asking about the on-disk path as opposed to the URL path. In that case, you want PHP's getcwd() function instead:

<?php
  echo  getcwd();
?>

Upvotes: 2

theraccoonbear
theraccoonbear

Reputation: 4337

Unless you track this yourself, I don't believe this would have a definition. Or rather, you're asking PHP to track something that you're somewhat arbitrarily defining.

The long and short of it is, if I'm understanding your question correctly, I don't believe what you're asking for exists, at least not as "core" PHP; a given framework may provide a "root" URL relative to a directory structure that it understands.

Does that make sense?

Upvotes: 1

Related Questions