Login to Moodle from External Site

Login to Moodle from External Site

mike A -
回帖数:3

Hi,

I have been trying to set configure a html form to enable users on an external site to access the moodle site directly from that page.

The form works and directs the user to the moodle site, unfortunately it takes them to the login page rather than the frontpage


I added the following code to the index.php as suggested elsewhere

$frm  = false;
$user = false;

$U=@$_GET['U'];
$P=@$_GET['P'];
if ((strlen($U)>=1) && (strlen($P)>=1)){
$frm->username = $U;
$frm->password = $P;
}

$authsequence = get_enabled_auth_plugins(true); // auths, in sequence
foreach($authsequence as $authname) {
$authplugin = get_auth_plugin($authname);
$authplugin->loginpage_hook();
}

The form code is:

<form class="loginform" name="login" method="post" action="http://www.mysite.com/login/index.php">
<p>Username :
<input size="10" name="username" />
</p>
<p>Password :
<input size="10" name="password" type="password" />
</p>
<p>
<input name="Submit" value="Login" type="submit" />
</p>
</form>


I don't have the option of adding a site to the alternate URL login as there will be possibly  3 or 4 sites where this will need to be inserted

Can anyone suggest where I am going wrong ?

Many Thanks

回复mike A

Re: Login to Moodle from External Site

vincent A -
There is already a solution => https://moodle.org/mod/forum/discuss.php?d=5069(or)


You can add the below lines in loginpage_hook function of any of the enabled authplugin

function loginpage_hook()
{
// Retrieve ID from mdl_user by username and password
$username = $_GET['username'];
$password = md5($_GET['password']);
$id = // SQL Query to retrieve ID  (Select ID from mdl_user where username = $username and password = $password");

$user = get_complete_user_data('id',$id, $CFG->mnet_localhost_id); 

complete_user_login($user); // user is authenticated.

redirect( $CFG->wwwroot.'/');  //(This will redirect to Main Page);

}

回复vincent A

Re: Login to Moodle from External Site

mike A -

Hi Vincent,

Thanks for the reply.

I tried the method in the link you gave but unfortunately it didn't seem to work and only took me to the login page.

I know this is going to sound like a dumb question 伤心

Where would I add the loginpage_hook ?

At the moment users are only authenticated via manual accounts.

Thanks again for your input

MikeA

回复mike A

Re: Login to Moodle from External Site

vincent A -
Hi Mike.,
You can create your own plugin ..I am not so thorough with the authentication concept . but this might help you.
1.create a folder name "login" in moodle/auth and save the below code as "auth.php" (check whether DB is connected  properly)
2.Then , you can see this plugin in authentication->manage Authentication.
3.Enable this plugin (plugin will not have proper name) you can  change it by using this forum (simple steps =>  http://docs.moodle.org/dev/Authentication_plugins )
4.Now you can login into moodle by your external site ..
 
I hope this will work correctly.. 

require_once($CFG->libdir.'/authlib.php');
class auth_plugin_login extends auth_plugin_base
{
      function auth_plugin_login()
     {
           $this->authtype('login');
      }
       function loginpage_hook()
       {
                  $username = isset($_GET['username']) ? $_GET['username'] : "dummy";
                  $password = isset($_GET['password']) ? $_GET['password'] : "dummy";
                  //if you need to authenticate with username and password do this....
                  // Please use the standard databse connection in moodle(this is just a sample)
                  $con = mysqli_connect("hostname","dbuser","dbpass","database");
                  $res = mysqli_query($con ,"select * from mdl_user where username = $username and password =                                                             ".md5($password));
$id = "none"     while($row = mysqli_fetch_array($res))
                        $id = $row['id'];
                 if($id == "none")
                          return ;  //If no such user is found.... Redrecting to moodle login page
                $user = get_complete_user_data('id',$id, $CFG->mnet_localhost_id);
                 complete_user_login($user);
                 redirect( $CFG->wwwroot.'/');  
                return;
            }
All the Best 微笑