Checking if user is logged in - External to Moodle.

Checking if user is logged in - External to Moodle.

by Dale Davies -
Number of replies: 5
I've been trying to get my head around this, I want to check if the user is logged into Moodle without including the Moodle config file.

One reason for this is so that I can ensure compatibility with other systems, Wordpress for example complains if I include config.php from Moodle.

Anyways, I have come up with the following code to do the checking, I stole the "unserialize_session" function from the Moodle core code and renamed it to suit (I know its cheating, sorry).

Can anyone tell me if there is a better way to do it than this? I'm concerned with security etc. I need to sanitize the contents of the cookie before using it in my query, but apart from that....


<?php
function unserialize_session($serialized_string) {
$serialized_string = urldecode($serialized_string);
$variables = array();
$a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
for( $i = 0; $i < count( $a ); $i = $i+2 ) {
$variables[$a[$i]] = unserialize( $a[$i+1] );
}
return( $variables );
}

$cookie = $_COOKIE['MoodleSession'];

$DB_PASSCHANGE->username = "xxxxxxx";
$DB_PASSCHANGE->password = "xxxxxxx";
$DB_PASSCHANGE->hostname = "localhost";

$dbh = mysql_connect($DB_PASSCHANGE->hostname, $DB_PASSCHANGE->username, $DB_PASSCHANGE->password);
$selected = mysql_select_db("xxxxxxxx",$dbh);

$query = mysql_query("SELECT * FROM mdl2_sessions2 WHERE sesskey='".$cookie."'");
$results = mysql_fetch_row($query);

$data = unserialize_session($results[5]);

$USER = $data['USER'];

if ($USER->id){
echo 'You are logged into Moodle.';
} else {
echo 'You are not logged into Moodle.';
}
?>


Many thanks,
Dale.

Average of ratings: -
In reply to Dale Davies

Re: Checking if user is logged in - External to Moodle.

by Dale Davies -
* Bump! *

Sorry, this got lost way down in the list pretty quickly, would still love to hear any input. Many thanks.
In reply to Dale Davies

Re: Checking if user is logged in - External to Moodle.

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Are you sure you want to do this at all? Being "logged in" to a web application is a rather shaky notion.
In reply to Howard Miller

Re: Checking if user is logged in - External to Moodle.

by Dale Davies -
Well, I'm not sure if I want to do this, was wondering if there is a better way to go about it? Perhaps it would help if I explained a little further what it is I actually want to achieve.

I have been redesigning our student intranet and Moodle so that they give the impression of being the same system, so this is integration at its most basic level I suppose, the student intranet will use Wordpress and a custom theme, Moodle will use a custom theme that looks exactly the same.

The students will never need to log into Wordpress and no session or user details will need to be passed between the two. However I want to show Moodle's "You are logged in as Dale Davies (Logout)" message at the top of the screen even when the user is on a page served up with Wordpress.

The easiest way that I can see of achieving this without having to overcome the problems of integrating the systems properly (which is something I want to avoid if possible) is to do something like I posted in my last message.

Perhaps there is something I'm missing, when I include Moodle's config.php file in a Worpdress page there are errors thrown out, I gather this is because of conflicting function names etc.

Can anyone think of a better way to do this, or suggest any improvements to the above script?

Many thanks.
In reply to Dale Davies

Re: Checking if user is logged in - External to Moodle.

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Probably the easiest way to do it would be a little snipped of Ajax in the wordpress site.

In moodle, make a simple script called something like local/getloginstring.php, which does something like

require_once('../config.php');
echo user_login_string($COURESE);

Then in your wordpress site, add a snippet of JavaScript that request that URL, and adds the returned content to the page where you want it.

Actually, you don't even need JavaScript, you just put the link to that in an iframe.
In reply to Tim Hunt

Re: Checking if user is logged in - External to Moodle.

by Dale Davies -
Thanks Tim, yes that works, I used javascript as I have a strange dislike of iframes. I guess sometimes its hard to see the most simple solutions. If you are interested I used Jquery....

$('#target_div').load('../getlogin.php');

The only caveat I can see with this approach however is that no login/logout link will be displayed if the user has JS turned off for some reason, obviously this doesnt happen in most modern web browsers.

Just out of interest, I'm interested in learning if the script I posted was anywhere near correct or if it was an "accident waiting to happen" (minus the obvious SQL injection where I put the cookie value straight into the query)?