Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moodle Accept Login from external site

Tags:

php

moodle

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:

  • User accesses the main site
  • User logs in
  • User clicks a link to Moodle
  • The user is automatically logged into Moodle without having to reenter their info
  • If the user bookmarks or directly accesses Moodle, they can still login directly there

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.

like image 272
awestover89 Avatar asked Apr 15 '13 14:04

awestover89


People also ask

How do I redirect a login page in Moodle?

The require_login() function will automatically redirect the home page to the login page.

How do I use an external database in Moodle?

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.

How do I customize my Moodle login page?

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.


1 Answers

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.

like image 142
awestover89 Avatar answered Sep 23 '22 12:09

awestover89