How to access the base URL?

Feed 9 posts, 3 voices

Aug 14, 2008 21:55
Avatar
286 posts

How can I access the base url for the site from a plugin?

right now i'm doing something like: $_SERVER['DOCUMENT_ROOT'] . '/testsite')

I think there must be a better way.

Thanks

 
Aug 15, 2008 04:07
Avatar
37 posts

Hi, The framework define a BASE_URL php constant, you can try to use it.

 
Aug 15, 2008 04:21
Avatar
286 posts

thanks, i'll give it a try!

 
Aug 15, 2008 06:59
Avatar
286 posts

when i use BASE_URL i get 'URL file access disabled' problem...

 
Aug 15, 2008 07:08
Avatar
37 posts

What are you trying to do ?

 
Aug 15, 2008 08:04
Avatar
286 posts

i'm trying to get to a subdirectory in the plugin. i need to require something at myplugin/php/foo.inc

and foo.inc needs to go back up and write to something at myplugin/cache

 
Aug 15, 2008 09:02
Avatar
37 posts

You can try to use relative path or use the FILE constant like:

require dirname(__FILE__).DIRECTORY_SEPARATOR.'php'.DIRECTORY_SEPARATOR.'foo.inc';

No ?

 
Aug 15, 2008 09:59
Avatar
396 posts

@ricks - BASE_URL is not the one you want to use... most hosting systems don't allow url based file access for security purposes. (which is good)

Like gido said, just use a relative path, something like one of these:

   require_once('php/foo.inc');
   require_once('../php/foo.inc');

Otherwise, you can try to use the FROG_ROOT variable as an absolute path, like:

   require_once(FROG_ROOT.'/frog/plugins/myplugin/php/foo.inc');

If that fails, do a test with the absolute filename... not nice for production, but try it and see if it works on your system:

   require_once('/home/myuser/www/frog/plugins/myplugin/php/foo.inc');
 
Aug 21, 2008 03:11
Avatar
286 posts

thanks for the suggestions, but do they offer an advantage over $_SERVER['DOCUMENT_ROOT'] ?

just wondering. thanks

 
Aug 21, 2008 07:53
Avatar
396 posts

@ricks - not a huge difference... main thing is, if you use FROG_ROOT or a relative file path, your require_once statement won't break.

If you use the SERVER version like in comment #9, you make your plugin dependent on server configuration instead of on Frog configuration. (I.e. Frog might work but your plugin might break)

Granted, the possibility of that happening is incredibly small... in other words: you choice. ;-)