wwwroot setting

wwwroot setting

by Joe Baud -
Number of replies: 3

I have Moodle set up on a server in my office. 

If I set up $CFG->wwwroot using my url, it works fine when I access remotely via the internet, but I can't connect when I'm in the office.

If I set up $CFG->wwwroot using "localhost", it works fine when I'm in the office, but I can't connect remotely via the internet.

Currently I have two copies of config in separate directories and copy the "localhost" indo my moodle directly when I need to work on the site.  Still developing content so at this point this isn't a problem but need a long term fix?

Thanks in Advance for any advice!

Average of ratings: -
In reply to Joe Baud

Re: wwwroot setting

by Reeny George -

I had a similar problem and it was solved using the following ( on  Apache 2.0) as suggested elsewhere on this site:

$CFG->wwwroot   = "http://".$_SERVER["HTTP_HOST"]."/moodle";

or  (if moodle is installed at  / )

$CFG->wwwroot   = "http://".$_SERVER["HTTP_HOST"]."/";   

The site can now be accessed from both the intranet and the internet. Please make sure that the two dots (.) are included .

Average of ratings: Useful (1)
In reply to Reeny George

Re: wwwroot setting

by Tom Essebier -

The above works great thanks.

Except my moodle cron scripts were also using config.php and when run in that context (ie. not via a web server like apache) the _SERVER array, unsurprisingly, contains nothing much - certainly not the index HTTP_HOST, leading to mail along the lines of:

PHP Notice:  Undefined index: HTTP_HOST in /usr/share/moodle/config.php on line 20

No biggie though, if you struck the same problem just check whether _SERVER has the key HTTP_HOST and if it doesn't, use localhost instead (or whatever takes your fancy). The array_key_exists function does that for us, combine it with the php ?: ternary (inline if?then:else), and you get:

$CFG->wwwroot   = "http://". (array_key_exists('HTTP_HOST', $_SERVER) ? $_SERVER["HTTP_HOST"] : "localhost") ."/moodle";

So the bit in the braces () gives us either $_SERVER["HTTP_HOST"] or "localhost" as the circumstances dictate. to which we prepend "http://" and post-pend "/moodle" (using php string concatenator '.') . localhost should work well since cron.php is local. And then its all better.

Well, at least cron doesn't send us any nasty-grams about config.php anymore.