We are adding Moodle to our site (different server, different sub-domain, but same main domain, and the servers are set up to be able to communicate with each other) and what we want to have it do is:
We already have it set up using db auth (the external database authentication) so the accounts from the main site also work for Moodle, I just have no idea how to get Moodle to accept the existing credentials and automatically log the user in.
The require_login() function will automatically redirect the home page to the login page.
In Moodle, go to Site administration > Plugins > Enrolments > Manage enrol plugins, find External Database in the list, enable it (click the closed-eye icon) and click Settings.
Logout, login and the screen will tell you it will install the new theme. Click on "Upgrade moodle database", go to Site administration -> Appearance -> Themes -> Theme selector and choose your theme so you can see it works and see the changes.
So I was able to get this resolved and thought I would share my solution in case anybody else has a similar problem in the future.
Since our Moodle site and main site are on the same domain, what I did was in the login script for our main site, I added the following code:
$postData = array('username' => $username, 'password' => $password);
$post = http_post_fields('http://moodle.example.com/login/index.php', $postData);
$headers = http_parse_headers($post);
foreach($headers['Set-Cookie'] as $cookie)
{
$details = http_parse_cookie($cookie);
foreach ($details->cookies as $name => $value)
setcookie($name, $value, $details->expires, $details->path, 'example.com');
}
Basically, I posted the login credentials to the moodle login script using http_post_fields, although cURL should work as well, parsed the headers to get the cookies Moodle set, then set those cookies myself using the basic domain instead of the more specific Moodle subdomain. This can cause some issues if the user has an existing cookie from the more specific subdomain, so be sure to delete any existing cookie with a name of MoodleSession.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With