General developer forum

Prevent multiple Login with Same ID

Picture of Swanand Taware
Prevent multiple Login with Same ID

Hi All,

Somehow i find out way to prevent multiple login with same id.

Below are Steps to follows:

Step 1:

Create Table in Moodle database

SELECT c.fullname Course_Name, as Assessment_Title,st.attempt Attempt, st.VALUE,FROM_UNIXTIME(st.timemodified,"%m-%d-%Y") Attempt_Date,FROM_UNIXTIME(mcc.timecompleted,"%m-%d-%Y") Completion_Date,

(CASE WHEN mcc.timecompleted IS NULL THEN 'Not Completed' ELSE 'Completed'

END) AS 'status'

FROM mdl_course_completions mcc

JOIN mdl_course AS c ON

JOIN mdl_scorm AS sc ON

JOIN mdl_user AS u

JOIN mdl_scorm_scoes_track AS st on st.scormid = AND st.userid =


Step 2: 

Add Below code into file lib.php of your theme directory.

function theme_klass_page_init(moodle_page $page) {

    global $DB, $CFG, $SESSION, $USER;

    $userid = $USER->id;

    $sessKey = $USER->sesskey;

    $rs = $DB->get_record_sql("SELECT count(*)AS count FROM `mdl_sessions` WHERE `userid` = '" . $userid . "'");

    echo "count===><br>";

    echo $rs->count;

    echo "<br>user id===><br>";

    echo $userid;

    if ($userid != 0) {

        if ($rs->count > 1) {

            echo "inside";//die('kasjhfskja');

            //if user login from another browser we have to logout that old user


            $var =  $CFG->wwwroot."/login/logout.php?sesskey=".$sessKey;

            header( 'Location: ' . $var );








Average of ratings: -
C'est moi :-)
Re: Prevent multiple Login with Same ID
Documentation writersParticularly helpful MoodlersTestersTranslators


Or you can go to "Site administration > Plugins > Authentication > Manage authentication" and specify a value for "Limit concurrent logins " (limitconcurrentlogins) wink


Average of ratings: Useful (5)
Picture of Jody Baty
Re: Prevent multiple Login with Same ID

I have enabled this setting. It worked previously but has recently stopped working on Moodle 3.1.5+ (Build: 20170316) . Are there any other settings that need to be adjusted for this to work?

Average of ratings: -